ulkaNCST
ulkaNCST

Reputation: 125

About C++ Exception Handling

I am reading literature on "C++ Exception Handling" (EH) from various resources. I have understood the concepts as well as the syntax. BUT On a larger scale I am confused .

  1. We write code to execute it completely. It is expected that the written code will execute in known specific way(s). If some part of code throws exception and it is handled and the program continues execution. But from throw point to catch function some function(s) did not execute,normally they should have been, then how come the program can continue further as it is not completely executed.

  2. I have come across statements that tells EH is used for error recovery. But i need some concrete example to see what error recovery means / how it is done especially in C++ EH.

Upvotes: 0

Views: 586

Answers (4)

MCM
MCM

Reputation: 11

Maybe the concept of "strong exception safety" provides somewhat an answer to both of your questions. A function which is strongly exception safe will either complete regularily or, if throwing, leave the state of the program as it was before it was called.

Disregarding for the moment if that can be achieved, let's assume your code is composed of strongly exception safe functions:

  1. Obviously execution can continue whenever an exception is caught. Just catch it at a point where there is an alternative route for execution. Ultimately just not executing the program at all. Of course you must admit failure to execute as a possibility. However, anything else would be perfection and somewhat unrealistic.
  2. I recommend Herb Sutter's "Exceptional C++" books (see http://www.gotw.ca/publications/xc++s.htm) or his Guru-of-the-Week postings, e.g. http://www.gotw.ca/gotw/059.htm

In general "error recovery" here means rolling back any changes. This usually can only be achieved by doing everything tentatively, eg. on a copy, until success is ensured.

Above that, error recovery is an architectural issue of designing your program with lots of places, where alternative executions routes exist.

Upvotes: 0

Amir Qayyum Khan
Amir Qayyum Khan

Reputation: 473

Note EH = Exception handling

We do EH to make sure that we do not get run time abnormal behavior of program, which can be due to some unexpected input/data at any point of execution or data corruption in processing. It depends on developer how he handles exception(s) i.e show any error message or correct the data and continue.

for example

class PlayGame{
private:
 /* variable */
public:
bool inputUserName() throw(){
   **** if user do not enter name throw exception
}
void play(){
********
}
void end(){
 ****
}
};

void game() throw(){
     PlayGame pg;
     pg.inputUserName();
     pg.play();
     pg.end();
}
void main(){
  /* one way of work */
  try{
     game()
  }catch (exception& e){
      cout<<"Error occour.. user name missing..\n";
  }

  /* second way can be */ 
try{
     game();
  }catch (exception& e){
     cout<<"Please enter name first...\n";
     game();
  }
}

This is good example of understanding EH in c++

Upvotes: 0

Jerry Coffin
Jerry Coffin

Reputation: 490058

When an exception is thrown, execution does not continue from the point immediately after where the exception was thrown. Rather, execution is transferred back to the "nearest"1 catch block that can catch the type of exception that was thrown. After the code in that catch block executes, execution continues in whatever code comes after it, just like usual. That might (eventually) lead back to the code where the exception was thrown, but 1) doesn't necessarily, and 2) if it does, re-runs the code leading up to that point, so if the exception isn't thrown (for whatever reason) the following code can be expected to execute normally (presuming, of course, that the other code is exception safe, so nothing has gotten mucked up in the process).

It's difficult to find good examples of error handling using exceptions (or otherwise, TBH), largely for some of the same reasons for using EH to start with -- the code to handle the exception properly is often (usually?) quite specific to the application at hand. For some small utilities and such, it may just print out an error message and exit. For a server that needs to keep running regardless of what happens, it might write something to the log, then continue executing.


  1. Here "nearest" has nothing to do with location in the source code -- it's based on execution. When code in a try block is executed, the corresponding catch block is set up stack-style. When an exception is thrown, the stack is unwound until it reaches a catch block that can handle the exception that was thrown.

Upvotes: 1

sandymatt
sandymatt

Reputation: 5612

Exceptions are safety nets, but they aren't meant to handle programmer errors (i.e. segfaults)

For example, let's say you're trying to read a file on disk that hasn't been written yet, but you know it will be at some point. Would you want to crash and burn the entire process? Probably not.

If you caught some sort of exception that indicated the file wasn't there yet, you could handle it appropriately by retrying at a later time, or logging some sort of a message.

Upvotes: 0

Related Questions