C++/advantages of exception handling
Expert: Eddie - 6/21/2005
Question1.does C language have exception handling mechanism.
if so , how ?
2. what are the advangages of exception handling ?
3. main()
{
int x = 7;
char *p = "i love you";
...
}
here , x is stored in stack .
p is also stored on stack , but it points to a string. where does the string stored ?
AnswerHello neo, thank you for the question.
The C language does not provide any kind of exception handling by default. The C++ language does however, and a lot of people find it extremely handy. With exception handling, you are offered the chance of catching an error, and actually correcting it, rather than crashing your program with an assert, old school style. Instead of just having a function return false, for failure, you could throw an exception, catch it in a handler block code, correct the situation, and keep going in your program. Here is an example using a basic std::exception thrown when a call to new fails:
int *p = NULL;
try
{
p = new int;
}
catch(std::bad_alloc &e)
{
std::cout << "New failed, default to malloc\n";
p = malloc(sizeof(int));
}
*p = 5;
And so on. Though I personally don't like exceptions, a lot of programmers use them, especially in Java programming.
For your third question, there phrase "i love you" is stored on a data block of the actual executable.
I hope this information was helpful.
- Eddie