Hunt this blog

Monday, May 25, 2009

Exceptions in C++

Exceptions provide a way to react to exceptional circumstances (like runtime errors) in our program by transferring control to special functions called handlers.

To catch exceptions we must place a portion of code under exception inspection. This is done by enclosing that portion of code in a try block. When an exceptional circumstance arises within that block, an exception is thrown that transfers the control to the exception handler. If no exception is thrown, the code continues normally and all handlers are ignored.

A exception is thrown by using the throw keyword from inside the try block. Exception handlers are declared with the keyword catch, which must be placed immediately after the try block:

// exceptions
#include
using namespace std;

int main () {
try
{
throw 20;
}
catch (int e)
{
cout << "An exception occurred. Exception Nr. " << e << endl;
}
return 0;
}

The code under exception handling is enclosed in a try block. In this example this code simply throws an exception:

throw 20;

A throw expression accepts one parameter (in this case the integer value 20), which is passed as an argument to the exception handler.

The exception handler is declared with the catch keyword. As you can see, it follows immediately the closing brace of the try block. The catch format is similar to a regular function that always has at least one parameter. The type of this parameter is very important, since the type of the argument passed by the throw expression is checked against it, and only in the case they match, the exception is caught.

We can chain multiple handlers (catch expressions), each one with a different parameter type. Only the handler that matches its type with the argument specified in the throw statement is executed.

If we use an ellipsis (...) as the parameter of catch, that handler will catch any exception no matter what the type of the throw exception is. This can be used as a default handler that catches all exceptions not caught by other handlers if it is specified at last:

try {
// code here
}
catch (int param) { cout << "int exception"; }
catch (char param) { cout << "char exception"; }
catch (...) { cout << "default exception"; }

In this case the last handler would catch any exception thrown with any parameter that is neither an int nor a char.

After an exception has been handled the program execution resumes after the try-catch block, not after the throw statement!.

It is also possible to nest try-catch blocks within more external try blocks. In these cases, we have the possibility that an internal catch block forwards the exception to its external level. This is done with the expression throw; with no arguments. For example:

try {
try {
// code here
}
catch (int n) {
throw;
}
}
catch (...) {
cout << "Exception occurred";
}

Types of Girls



HARD DISK GIRLS:
she remembers everything, FOREVER


RAM GIRLS:
she forget about you, the moment turn her off


WINDOW GIRLS:
everyone know that she can't do a thing right, but no one can live
without her.


SCREENSAVER GIRLS:
She is good for nothing but at least she is fun


INTERNET GIRLS:
Difficult to access


SERVER GIRLS:
Always busy when you need her.


MULTIMEDIA GIRLS:
She make horrible thing look beautiful


CD-ROM GIRLS:
She is always faster and faster.


EMAIL GIRLS:
Every ten things she says, eight are nonsense .


VIRUS GIRLS:
Also known as "wife'' when you are not expecting her, she comes,
install herself and uses all your resources. If you try to uninstall
her you will lose something, if don't try uninstall her you will
lose everything...

Diet Tips For Healthier Eating



Making a few small shifts in habits and routines may help you to achieve your weight loss and diet goals. A first tip that comes to mind is eating directly out of a bag or box. This can settle into a sort of mindless routine and it's very hard to control your intake if you are watching television or reading a book and just reaching for a few more out of that store container. Chips and popcorn put into a bowl is a good way to control the amount. Once you are finished, you are finished. A movie popcorn can contain 1,500 calories.

Do eat more slowly. It takes about twenty minutes for your brain to tell your stomach that it's full. If you eat so fast that you are just throwing things into your mouth mindlessly, there is no chance for the signal to switch on, or it could switch on late. Also, eating more slowly lets your body metabolize the food better. Try to eat with no distractions -- and sitting at the dining table will enable you to concentrate on the meal in front of you.

Most of us love dessert and we don't have to miss out on one after a meal. Substituting yoghurt for ice cream or a piece of fruit for a pie slice, or just cutting any dessert in half will really help. Some nuts or cheese or grapes for instance, can serve as added protein and fiber yet give the sense of a meal "finisher". Use of a vaporizer with a peppermint or other soothing or sweet herb is a great no-calorie way to get a sense of a dessert, without actually eating one -- and it can relax you also, which is better for digestion.

After eating your meal, wait at least ten minutes before reaching for seconds, and have a glass of water before every meal. If you are eating out, ask for a take home box before you start eating and put half of your meal into the box. Out of sight and out of mind really works. A lot of small lifestyle changes really add up. If you can start one or two at a time, then add more later as you go along, the transition to a healthier way of eating will come much easier. As always, stay healthy and happy.