You are here:

C++/advantages of exception handling

Advertisement


Question
1.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 ?  

Answer
Hello 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

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Eddie

Expertise

I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.

Experience

I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2016 About.com. All rights reserved.