x

C++ Exception Handling

PREV     NEXT

C++ Exception Handling:


What is Exception Handling?

  • An exception is an error that arises during the execution of a program. In C++, the exception is an event which is occurred at the runtime.
  • The exceptions are derived from std::exception class. It is a runtime error which can be handled and if they are not handled then the exception prints exception message and terminate the program.
  • In C++, the exception handling consists of three keywords: try, catch and throw.

Let us discuss them:

Try:

The try block identifies a block of code which can throw the exception. There can be one or more catch blocks after a try block.

Catch:

The catch block identifies the block of code that is executed when a particular exception is thrown. The program catches the exception with an exception handler at the place in a program where you want to handle the problem.

Throw:

It is used to throw an exception and it is also used to show those exceptions that a function throws which cannot be handled by them.

Let us have a look at the syntax:

try {

   // protected code

} catch( ExceptionName e1 ) {

   // catch block

} catch( ExceptionName e2 ) {

   // catch block

} catch( ExceptionName eN ) {

   // catch block

}


Let us have a look at the example:

What is the advantage of using Exception?

  • It helps programmers to create reliable systems.
  • Functions can handle only the exceptions they choose i.e., a function can throw many exceptions, but may choose handle only some of them.
  • We can handle the exceptions outside the regular code by throwing the exceptions from a function definition or by re-throwing an exception.
  • It helps to separate the exception handling code from the main logic of program.

Standard Exceptions in C++

In C++, it provides a list of standard exceptions which are defined in <exception> which we can use in our programs and they are arranged in a parent-child class hierarchy.                                                                                                                                   
Let us discuss these exceptions in detail:

  • std::exception – The exception and parent class of all the standard C++ exceptions.
  • std::logic_error – This exception can be detected by reading the code.
  • std::domain_error – This exception is thrown when an invalid domain is used.
  • std::invalid_argument – This exception is thrown due to invalid arguments.
  • std::out_of_range – This exception is thrown due to out of range which means size requirement exceeds the memory allocation.
  • std::length_error – This exception is thrown due to length error.
  • std::runtime_error – This exception occurs during runtime and cannot be detected by reading the code.
  • std::range_error – This exception occurs when you try to store a value out of range.
  • std::overflow_error – This exception occurs due to arithmetic overflow occurs.
  • std::underflow_error – This exception occurs due to arithmetic underflow occurs.
  • std::bad_alloc – This exception occurs when memory allocation fails by new( ).
  • std::bad_cast – This exception occurs when dynamic cast fails.
  • std::bad_exception – This exception is designed to be listed in the dynamic exception specifier.
  • std::bad_typeid – This exception is thrown by typeid.

PREV     NEXT



Like it? Please Spread the word!