Exception Handling in C++

 Introduction to Exception Handling

Exceptions offer a way to react to exceptional situations in programs by conveying control to distinct functions named handlers.

To catch exceptions, a part of code is located under exception inspection. i.e. try-block. if an exceptional conditionascends within that block, an exception is thrown that handovers the control to the exception handler.in case, no exception is thrown, the code lastsusually and all handlers are overlooked.

it is thrown by using the throw keyword from inside the try block. Exception handlers are stated with the keyword catch, which must be placed proximately after the try block:

 Need of exceptions

  • In old-style error handling codes, there are constantly if else conditions to handle errors. These conditions and the code to knob errors get mixed up with the usual flow. This makes the code less clear and maintainable. With try catch blocks, the code for error handling turn out to bedistinct from the normal flow.
  • A function can throw several exceptions, but may select to handle some of them. The additional exceptions which are thrown, but not fixed can be handled by caller. If the caller picks not to catch them, then the exceptions are controlled by caller of the caller.
    In C++, a function can identify the exceptions that it throws using the throw keyword. The caller of this function must grip the exception in some way.
  • We can makeaorder of exception objects, group exceptions in namespaces or else classes, classify them according to types.

Example:

Output:

Before try
Inside try
Exception Caught
After catch (Will be executed)

Try block is essentially a code block which backgrounds the code where there is a chance of incidence of error or else exceptions.

Syntax :

Code:

try

{

result = numerator/denominator;

cout<<“\nThe result of division is:” <<result;

}

  1. Catch Block
  • Catch block implements only if an exception happens inside the try block.
  • if an exception occurs, a message is passed to the resultant catch block along with some parameters.
  • Then the statement in the consistent catch block. All of our exception handling logic should be confined in the catch block.
  • a try block will at all times be followed by a catch block.

Example:

Code:

catch(int num) {

cout<<“You cannot enter “<<num<<” in denominator”;

}

  1. Throw Statement
  • normally, we pass a message from the catch block to try block that an exception has arisen .you will have to catch this exception.
  • In this regard,We send this message from the try block to a catch block throughdistinctive statement named ‘throw’.
  • Throw statement essentially throws the exception to catch block and passes the factor to catch block.
  • Throw statement cannot be stated outside the try block, it has to be inside the try block.

Example:

Code:

try {

if(denominator == 0) {

throw denominator;

}

result = numerator/denominator;

cout<<“\nThe result of division is:” <<result;

}

here, we have indicated an if condition which positions that if denominator is zero, an exception have to be thrown using throw statement .we are passing denominator as the factor to the catch block.
Standard Exceptions available in C++

  • The C++ Standard library offers a base class precisely designed to speak out objects to be thrown as exceptions. It is named std::exception and is defined in the <exception>

This class has a virtual member function called what that returns a null-terminated character order and that can be overwritten in derived classes to concealment some kind of clarification of the exception.

here,We have placed a handler that catches exception objects by reference .Consequently this catches also classes derived from exception.
Wholly exceptions thrown by components of the C++ Standard library throw exceptions imitative from this exception class. As follows,

exception description
bad_alloc thrown by fresh on allocation failure
bad_cast thrown by dynamic_cast when it flops in a dynamic cast
bad_exception thrown by assured dynamic exception specifiers
bad_typeid thrown by typeid
bad_function_call thrown by bare function objects
bad_weak_ptr thrown by shared_ptr when passed a corrupt weak_ptr

Also deriving from exception, header <exception> describes two generic exception types that can be inbred by custom exceptions to report errors:

exception description
logic_error error linked to the internal logic of the program
runtime_error error noticed during runtime

Example:

here, the exception handler in this example is a bad_alloc. As bad_alloc is derived from the standard base class exception, it can be fixed.

 Advantages of exception handling 

  • there is a precisedifference between the normal code and the error-handling code. This separation results in less difficult and more clear code.
  • It is also more well-organized, in the sense that the testing of errors in the normal execution path is not desirable, and thus wantsless CPU cycles.
  • A logical grouping will allow us to handle associated exceptions using a single exception handler. Therefore, an exception handler can catch exceptions of the class specified by its parameter, or else else can catch exceptions of any of its sub-classes.
  • There is the knack to transmit errors up the call stack. This is changed from traditional error-handling mechanisms in which the return values are tested and spread to the calling function.
  • All code that is to be controlled when an exception advances is placed in the catch block, which is not part of the original code.
  • NEED SOME MORE POINTS

 

Leave A Comment

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