You are on page 1of 9

Errors and Error Handling

An error is any unexpected result


obtained from a program during execution.

Unhandled errors may manifest


themselves as incorrect results or
behavior, or as abnormal program
termination.
Errors should be handled by the
programmer, to prevent them from

An exception is a event, which occurs


during the execution of a program that
disrupts the normal flow of the programs
functions. The runtime system searches the
call stack for a methods that contains a
block of code that can handle the exception.
This block of code is called an exception
handler. An exception handler is considered
appropriate if the type of exception object
thrown matches the type that can be
handled by the handler.

There are five keywords that could


be used for exception handling:
The try block
The catch block
The finally block
The throw statement

The try block


The try step in constructing an exception handler
is to enclose the code that might throw an
exception within a try block.
Syntax:
Try
{
Code
}
catch and finally blocks..

The catch block


Exception handler with a try block are associated
with one or more catch blocks directly after the
try block.
Syntax:
Try
{
}Catch (Exception Type name){
} Catch (Exception Type name){
}

The finally block


The finally block always executes when the
try block exits. This ensure that the finally
block is executed even if an unexpected
exception occurs. Finally creates a block of
code that will be executed after a try/catch
block has completed and before the code the
code following the try/catch block. The finally
block will execute whether or not an exception
is thrown.

The throw statement


Throw object of super class throw able or a sub
class of it. Whenever we get the throw statement
in the program the execution is stopped
immediately after this and the nearest possible try
block is checked to find out if there is any catch
statement that may match the type of exception
which has come, if it doesnt match, the control
goes to another try and so on.

General Syntax of throw is :


throw new type of Exception

Conclusion
Exceptions are used in Separating
Error-Handling code from Regular
code.
Exceptions are used to Propagate
Errors Up the Call Stack.
Exceptions help in Grouping and
Differentiating Error types.

You might also like