Exceptions Handling - PPT
Exceptions Handling - PPT
Compile Time Errors – Errors caught during compiled time is called Compile time errors.
Compile time errors include library reference, syntax error or incorrect class import.
Run Time Errors - They are also known as exceptions. An exception caught during run time
creates serious issues.
Exception handling is the process of handling errors and exceptions in such a way that they
do not hinder normal execution of the system.
For example, User divides a number by zero, this will compile successfully but an exception
or run time error will occur due to which our applications will be crashed.
In C++, Error handling is done using three keywords:
try
catch
throw
Syntax:
try
{
//code
throw parameter;
}
catch(exceptionname ex)
{
//code to handle exception
}
try block
The code which can throw any exception is kept inside(or enclosed in) a try block.
Then, when the code will lead to any error, that error/exception will get caught inside the catch
block.
catch block
catch block is intended to catch the error and handle the exception condition.
We can have multiple catch blocks to handle different types of exception and perform different
actions when the exceptions occur.
For example, we can display descriptive messages to explain why any particular excpetion
occured.
throw statement
It is used to throw exceptions to exception handler i.e. it is used to communicate information
about error.
A throw expression accepts one parameter and that parameter is passed to handler.
throw statement is used when we explicitly want an exception to occur, then we can use throw
statement to throw or generate that exception.
• If the try block throws an exception then program control leaves the block and
enters into the catch statement of the catch block.
• If the type of object thrown matches the argument type in the catch statement,
the catch block is executed for handling the exception.
Try Block
Catch Block
} // end main
Division by zero exception
Division by zero exception: An exception can be thrown from outside try block aslong as it is thrown by a function that is called from try block
Handling Derived Class Exceptions
Handling Derived Class Exceptions