Exception Handling in C++

By  Falak Hasija    9 - 17 August, 20
exception handling c++

Share article on social media


One of the fundamental aspects of programming is error handling. A program needs a facility to handle potential errors and abnormal conditions. 

Several types of errors can occur when we run a program. Some occur when the user enters bad data. Some, such as a disk read error, occurs when the hardware fails. Some occur when a programming error, such as a bad pointer, causes the program to fail.

HANDLING ERRORS:

TRADITIONAL ERROR HANDLING: Traditional error handling uses the return value from a function to communicate status. If the function is successful, it returns 0 to indicate that there were no errors.

If an error occurs, the function returns a unique number to identify the error. 

TRADITIONAL ERROR LOGIC:

#include

using namespace std;

int main()

{

cout<<"Enter the dividend: ";

double dividend;

cin>>dividend;

cout<<"Enter the divisor: ";

double divisor;

cin>>divisor;

if(divisor==0)

{

cout<<"**Error 100:divisor 0";

exit(100);

}

double quotient=dividend/divisor;

cout<<"Quotient is: "<<quotient<

return 0;

}    //main

 

Results;

Enter the dividend: 7

Enter the divisor: 0

**Error 100:divisor 0

USING THE EXCEPTION HANDLING MECHANISM:

A better way of handling is to use the exception handling mechanism of C++.

What is an exception?

An exception is an event that signals the occurrence of an error. 

Note: The error detection logic is coded in a special construct called the try statement.

The error handling logic is coded in another construct called the catch statement.


TRY AND CATCH STATEMENT

Try Statement: This statement allows us to encapsulate the codes that may create an exception. It is designed as a compound statement; the braces are required only if there is one statement. Whenever an error, such as divide by zero, is detected, the try statement transfers control to the catch statement by throwing an exception. If no error is detected, the try statement executes normally and the catch statement is ignored.

Throw Statement: This statement inside the try statement raises an exception by throwing a variable object to the catch statement.

Catch Statement: This statement is an exception handler that receives the object raised the throw statement. It contains whatever logic is necessary to handle the exception. Once again it is a compound statement, and the braces are required. If no catch statement is provided when an exception is thrown, the program aborts. The catch statement requires an object whose type matches the type of variable or object thrown in the try statement.

CATCH DIVIDE BY ZERO:

#include

using namespace std;

int main()

{

cout<<"Enter the dividend: ";

double dividend;

cin>>dividend;

cout<<"Enter the divisor: ";

double divisor;

cin>>divisor;

try

{

if(divisor==0.00)

throw divisor;

double quotient=divided/divisor;

cout<<"Quotient is: "<

}//try

catch(double& error)

{

cout<<"**Error 100: divisor: 0";

exit(100);

}//catch

return 0;

}//main

 

PROPAGATING EXCEPTION:

One more way to handle errors is to throw an error in a separate function and be caught in the calling function.

 
 
DIVIDE WITH EXCEPTION HANDLING:

#include

using namespace std;

double divide (double quot, double dvar);

int main()

{

cout<<"Enter the dividend: ";

double dividend;

cin>> dividend;

cout<<"Enter the divisor: ";

double divisor;

cin>>divisor;

try

{

double quotient = divide (dividend, divisor);

cout<<"Quotient is: "<

}//try

catch (double& error)

{

cout<<"**Error 100: divisor 0";

exit(100);

}//catch

cout<<"End of exception handling test\n";

return 0;

}//main

double divide (double dvnd, double dver)

{

if(dvar==0)

throw dvar;

return dvnd/dver;

}//divide

 

Results:

Run 1:

Enter the dividend: 7

Enter the divisor: 0

**Error 100: divisor 0

Run 2:

Enter the dividend: 7

Enter the divisor: 3

End of exception handling test

 

Re-Throwing an exception:

Sometimes, the exception handler cannot completely handle the exception. When this happens, it handles as much as it can and re-throws the exception to the higher-level function that called it. 

A re-throw statement uses throw without an object identifier.

 

                                                                                                           KEEP LEARNING!