0% found this document useful (0 votes)
2 views31 pages

Exception Handling

Exception handling in Java addresses unwanted events that disrupt program flow, providing meaningful error messages to users instead of system-generated ones. There are two types of exceptions: checked exceptions, which are verified at compile-time, and unchecked exceptions, which are not. The try-catch structure allows programmers to handle exceptions gracefully, while the finally block ensures that critical code executes regardless of whether an exception occurs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views31 pages

Exception Handling

Exception handling in Java addresses unwanted events that disrupt program flow, providing meaningful error messages to users instead of system-generated ones. There are two types of exceptions: checked exceptions, which are verified at compile-time, and unchecked exceptions, which are not. The try-catch structure allows programmers to handle exceptions gracefully, while the finally block ensures that critical code executes regardless of whether an exception occurs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

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

There are two types of exceptions in Java:


1)Checked exceptions
2)Unchecked exceptions
1:Checked
Exceptions
(Depends on
Outer
Resources)
All exceptions other than Runtime Exceptions are known as Checked exceptions as the
compiler checks them during compilation to see whether the programmer has handled them
or not. If these exceptions are not handled/declared in the program, you will get
compilation error.
For example:
Unchecked Exceptions

•Runtime Exceptions are also known as Unchecked Exceptions. These exceptions


are not checked at compile-time so compiler does not check whether the
programmer has handled them or not but it’s the responsibility of the programmer
to handle these exceptions and provide a safe exit.
For example:
□ ArithmeticException
□NullPointerException
□ArrayIndexOutOfBoundsException
Try block

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

1. A single try block can have any number of catch


blocks.
A generic catch block can handle all the exceptions. Whether it
2. ArrayIndexOutOfBoundsException
is OR ArithmeticException OR
NullPointerException OR any other type of exception.
3. If no exception occurs in try block then the catch blocks are completely ignored.
4. Corresponding catch blocks execute for that specific type of
exception:
▪catch(ArithmeticException e) is a catch block that can
hanlde ArithmeticException
catch(NullPointerException e) is a catch block that can
handle NullPointerException
Example of Multiple catch blocks
Nested try catch block in Java
finally block – Exception Handling

• Java finally block is a block used to execute important


code such as closing the connection, etc.
• Java finally block is always executed whether an
exception is handled or not. Therefore, it contains all
the necessary statements that need to be printed
regardless of the exception occurs or not.
• The finally block follows the try-catch block.
Why use Java finally block?

•finally block in Java can be used to put "cleanup"


code such as closing a file, closing connection, etc.

•The important statements to be printed


can be placed in the finally block.
Syntax of finally Block
Example of finally Block
Example 2

You might also like