C++ throwing and Catch Exception

C++ Throwing and Catch Exception

When an exception that is desired to be handled is detected , it is thrown using the throw statement(throw keyword) in one of the following forms show below

throw(exception);
throw exception;
throw; // this statement for re-throwing an exception

The operand object exception may be of any type, including constant, it is also possible to throw objects not intended for error handling

Whenever an exception is thrown, it will be caught by the catch statement(block) associated with the try block. that is the control exist the current try block, and is transferred to the catch block after that try block

Throw point can be in a deeply nested scope within a try block or in a deeply nested function call. in any one case the control is transferred to the catch statement

CATCHING MECHANISM

As stated earlier, code for handling exception is included in catch blocks. A catch block looks like any function definition who catch the exception and is of the form

catch(type arg)
{
// statement for
// managing exception
}

The type indicates the type of the exception that catch block handles. The argument arg is an optional parameter name.

Note : the exception handling code is placed between the two braces. The catch statement or block catches an expression whose type matches with the type of catch argument. When it is caught, the code inside the catch block is executed

If the parameter in the catch statement is named, then the parameter can be use in the code of Exception handling. After execution the handler, the control goes to The statement immediately following the catch block

due to mismatch, if an exception is not caught, abnormal program termination will occur. It is important to note that the catch block is simply skipped if the catch statement does not catch an exception

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise