Professional Documents
Culture Documents
6 RE-THROWING EXCEPTION
7 SPECIFYING EXCEPTIONS
Similar to errors, exceptions are also of two types. They are as follows:
1 Synchronous exceptions
2 Asynchronous exceptions
The goal of exception handling is to create a routine that detects and
sends an exceptional condition in order to execute suitable actions.
The routine needs to carry out the following responsibilities:
1 Detect the problem
2 Warn that an error has been detected
3 Accept the error message
4 Perform accurate actions without troubling the use
Syntax of catch
try
{
Statement 1;
Statement 2;
}
catch ( argument)
{
statement 3; // Action to be taken
}
When an exception is found, the catch block is executed.
The catch statement contains an argument of exception type, and it
is optional.
When an argument is declared, the argument can be used in the
catch block.
In case no exception is caught, the catch block is ignored.
Exception Handling November 16, 2022 7 / 14
Guidelines for exception handling
Example
RE-THROWING EXCEPTION
Specifying Exceptions
The specified exceptions are used when we want to bind the function to
throw only specified exceptions.
Syntax:
Data-type fucntion_name (parameter list)throw (data type list)
{
Statement 1;
Statement 2; Function definition
statement 3;
}
The data type list indicates the type of exception that is permitted to
be thrown.
Throwing any other type of exceptions in program it will cause
abnormal program termination.
If we want to deny a function from throwing an exception, declaring
the data type list void
throw(); (void or vacant list )
Exception Handling November 16, 2022 14 / 14