Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Exception Handling in C++

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 93
    Comment on it

    While writing programs we always have a chance of exception to occur that needs to be handled in our program.

     

    For exception handling we use the try catch blocks and finally blocks.

    • throw: A program throws exception when it occurs.

    • catch: A program catches the exception with its type inside this block.

    • try: A try block identifies the exception that occurs with its type.

     

    Throwing Exceptions:

    Exceptions can be thrown from anywhere in the program using this keyword.

     

    double division(int a, int b)
    {
       if( b == 0 )
       {
          throw "Division by zero condition!";
       }
       return (a/b);
    }

     

    Catching Exceptions:

    try
    {
       // protected code
    }catch( ExceptionName e )
    {
      // code to handle ExceptionName exception
    }

     

     

    C++ Exceptions Hierarchy

     

    Exception Description
    std::exception An exception and parent class of all the standard C++ exceptions.
    std::bad_alloc This can be thrown by new.
    std::bad_cast This can be thrown by dynamic_cast.
    std::bad_exception This is useful device to handle unexpected exceptions in a C++ program
    std::bad_typeid This can be thrown by typeid.
    std::logic_error An exception that theoretically can be detected by reading the code.
    std::domain_error This is an exception thrown when a mathematically invalid domain is used
    std::invalid_argument This is thrown due to invalid arguments.
    std::length_error This is thrown when a too big std::string is created
    std::out_of_range This can be thrown by the at method from for example a std::vector and std::bitset<>::operator[]().
    std::runtime_error An exception that theoretically can not be detected by reading the code.
    std::overflow_error This is thrown if a mathematical overflow occurs.
    std::range_error This is occured when you try to store a value which is out of range.
    std::underflow_error This is thrown if a mathematical underflow occurs.

     

     

    #include <iostream>
    #include <exception>
    using namespace std;
    
    struct MyException : public exception
    {
      const char * what () const throw ()
      {
        return "C++ Exception";
      }
    };
     
    int main()
    {
      try
      {
        throw MyException();
      }
      catch(MyException& e)
      {
        std::cout << "MyException caught" << std::endl;
        std::cout << e.what() << std::endl;
      }
      catch(std::exception& e)
      {
        //Other errors
      }
    }

     

    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: