Exceptions - C++ Tutorials - learnit

Home Top Ad

Post Top Ad

Saturday, June 27, 2020

Exceptions - C++ Tutorials

Exceptions - C++ Tutorials

Exceptions - C++ Tutorials

    One of the advantages of C++ over C is Exception Handling. Exceptions are run-time anomalies or abnormal conditions that a program encounters during its execution. There are two types of exceptions: a) Synchronous, b) Asynchronous. C++ provides following specialized keywords for this purpose.


Try: Try: represents a block of code that can throw an exception.

Catch:represents a block of code that is executed when a particular exception is thrown.

Throw:Used to throw an exception. Also used to list the exceptions that a function throws, but Following are main advantages of exception handling over traditional error handling.


    1) Separation of Error Handling code from Normal Code: In traditional error handling codes, there are always if else conditions to handle errors. These conditions and the code to handle errors are mixed up with the normal flow. This makes the code less readable and maintainable. With try catch blocks, the code for error handling becomes separate from the normal flow


    2) Functions/Methods can handle any exceptions they choose: A function can throw many exceptions, but may choose to handle some of them. Caller can handle the other exceptions, which are thrown, but not caught. If the caller chooses not to catch them, then caller of the caller handles the exceptions.


    In C++, a function can specify the exceptions that it throws using the throw keyword. The caller of this function must handle the exception in some way (either by specifying it again or by catching it)


    3) Grouping of Error Types: In C++, both basic types and objects can be thrown as exception. We can create a hierarchy of exception objects, group exceptions in namespaces or classes, categorize them according to types.

Syntax:

Try
{
Throw 40;
}
Catch(int i)
{
statment
}

Throw 40

A throw expression accepts one parameter (in this case the integer value 40), which is passed as an argument to the exception handler.


The exception handler is declared with the catch keyword immediately after the closing brace of the try block. The syntax for the catch is similar to a regular function with one parameter. The type of this parameter is very important, since the type of the argument passed by the throw expression is checked against it, and only in the case they match, the exception is caught by that handler.


Multiple handlers (i.e., catch expressions) can be chained; each one with a different parameter type. Only the handler whose argument type matches the type of the exception specified in the throw statement is executed.


If an ellipsis (...) is used as the parameter of catch, that handler will catch any exception no matter what the type of the exception thrown. This can be used as a default handler that catches all exceptions not caught by other handlers:


Example

#include
using namespace std;
  class person{
    public:
      person(){cout<<"My name is VANNAK VANNARO"<<endl;
      }
      person(){cout<<"I Live in Phnom Penh"<<endl;
      }};
int main()
{
    try{
      person p1;
      throw 40;
      }
      catch(int i){
      cout<<"Years" <<i < < endl;
}}

Please Watching My Video is Below

2 comments:

Post Top Ad