Exception handling in C++

In this tutorial, we will learn about exception handling in c++. How it is handled in c+ +. let us see with an example.

Exception Handling in C++

It has two approaches to exception handling: They follow each function call with a test for errors and they use setjmp and longjmp to intercept error conditions.
The first approach , which uses something like errno and null or ERROR function returns.
The setjmp/longjmp approach is closer to what C++ exception handling strives for: an orderly and automatic way to unwind the stack to a state that was recorded at a specified place higher in the function-call hierarchy.

Further, C++ exception handling, which involves try, catch and throw blocks, presents a more orderly and readable way to view exception handling than C’s setjmp/longjmp mechanism does.

The try Block:

C++ functions that can sense and recover from errors exception from within a try block that looks like this:

try
{  
// C++ statements
}

Code executing outside any try block cannot detect or handle exceptions.Try block typically calls other functions that are able to detect exceptions.

The catch Exception Handler:

A try block is followed by a catch exception handler with a parameter list, as shown here:

try
{
// C++ statements
}
catch (int err)
{
// error -handling code
}

Multiple catch handlers with different parameter lists can exist.

try
{
// C++ statements
}
catch (int er)
{
// error-handling mode
}
catch(char *msg)
{
// error-handling code with cahr*
}

The catch handler is identified by the type in its parameter list. The parameter in the catch parameter list needn’t be named. If the parameter is named, it declares an object with that name and the exception-detection code can pass a value in the parameter. If the parameter is unnamed the exception -detection code can jump to the catch exception handler merely by naming the type.

The throw Statement:

To detect an exception and jump to a catch handle, a c++ functions issues the throw statement with a datatype that matches the parameter list of the proper catch handler :

throw “An error has occurred “;

This throw statement would jump to the catch exception handler function that has the char* parameter list.

The throw statement unwinds the stack, cleaning up all objects declared within the try block by calling their destructors. Next, throw calls the matching catch handler, passing the parameter object.

The try/throw/catch Sequence:

The following code begins to bring it all together:

class Bummer{};
int main()
{
try
{
foo();// an exception might be thrown
}
catch(Bummer)
{
//an exception is caught
}
}
void foo()
{
 //....
throw Bummer();
}

Catch handlers and their matching throw statements can have a parameter of any type. For example:

catch(ErrorCode ec)

{

…….

}// —-

throw ErrorCode(123);

Catch -all Exception Handlers:

A catch handler with ellipses for a parameter list shows next, catches all uncaught exceptions:

catch(...)
{
// error -handling code
}

In a group of catches associated with a try block, the catch-all handler must appear list.

Throwing an Exception from a Handler:

try
{
try
{ // ----
}
catch (...)
{
throw; //rethrows the exception
}
}
catch(...)
{
// catches the rethrown exception
}

Program to implement Throwing and Catching Exceptions  in C++

#include<iostream>
using namespace std;
void foo();
class Bummer();
int main()
{
try // try block
{
cout<<"calling foo\n";
foo();
cout<<"return from foo\n";
}
catch(Bummer) //catch exception handler
{
//  ... error-handling code
cout<<"catching Bummer\n";
}
cout<<"done\n";
}
void foo()
{
int error =1;
 //C++ statements to do stuff
if(error)
{
cout<<"throwing Bummer\n";
throw Bummer();// Throw an exception
}
}

Output:

calling foo
throwing Bummer
catching Bummer
done

also read:

Leave a Reply

Your email address will not be published. Required fields are marked *