Exception Handling
Exception Handling
Exception:
•An Exception is an unwanted event (Run Time Errors) that interrupts the normal
flow of the program. When an exception occurs program execution gets
terminated.
• In such cases we get a system generated error message.
•By handling the exceptions java provides a meaningful message to the
user about the issue, rather than a system generated message, which may not
be understandable to a user.
Exception Handling
•If an exception occurs, which has not been handled by programmer then
program execution gets terminated and a system generated error message is
shown to the user.
Advantage of Exception Handling
•Exception handling ensures that the flow of the program doesn’t break when an
exception occurs.
•For example: if a program has bunch of statements and an exception occurs
mid way after executing certain statements then the statements after the
exception will not execute and the program will terminate abruptly.
Types of Exceptions
The try block contains set of statements where an exception can occur. A try block
is always followed by a catch block, which handles the exception that occurs in
associated try block. A try block must be followed by catch blocks or finally block
or both.
Catch Block
•A catch block is where you handle the exceptions, this block must follow the try
block. A single try block can have several catch blocks associated with it. You can
catch different exceptions in different catch blocks. When an exception occurs in
try block, the corresponding catch block that handles that particular exception
executes. For example if an arithmetic exception occurs in try block then the
statements enclosed in catch block for arithmetic exception executes.
Syntax of “try – catch” in java
Multiple “catch” blocks in Java