0% found this document useful (0 votes)
19 views21 pages

9.1 Exceptions

Exceptions in Java occur at runtime due to errors, like division by zero. Not handling exceptions properly can crash programs. Exception handling avoids resource leaks by ensuring resources are returned when exceptions occur. Exceptions are caught in catch blocks while finally blocks guarantee cleanup code runs regardless of exceptions.

Uploaded by

Anuj Mourya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views21 pages

9.1 Exceptions

Exceptions in Java occur at runtime due to errors, like division by zero. Not handling exceptions properly can crash programs. Exception handling avoids resource leaks by ensuring resources are returned when exceptions occur. Exceptions are caught in catch blocks while finally blocks guarantee cleanup code runs regardless of exceptions.

Uploaded by

Anuj Mourya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

What is an Exception ?

Exception

•Errors encountered during the


execution of a java program.
•Occurs at runtime.
•E.g division by zero causing
an error during execution.
Not handling the errors or
“exceptions” properly can cause
the program or system to crash.

Error messages must be


displayed as an exception occurs,
so that the abrupt termination of
a program could be stopped.
Purpose of
Exception Handling
Exception Handling - Purpose

•If a program gets terminated


due to an exception, all
resources allocated by the
system are left in the same
state. It can cause a
RESOURCE LEAK.
Exception Handling - Purpose

•For example, during an I/O


operation in a file, an
unhandled exception can
cause the program to abort
without closing the file. This
may damage the file.
How to avoid Resource Leak ?

•To avoid resource leak, all the


resources allocated to the
system have to be returned to
the system.
•Exception Handling
mechanism smoothens the
resource de-allocation.
Handling Exceptions
Handling Exception

•When an exception occurs, an object


representing that exception is
created.
•The object is then passed to the
method that handles the exceptions.
•This object contains detailed
information about the exception. This
info can be utilized accordingly
Exceptions can be generated
by :
• Run-time environment
• Manually generated by code.

• The class “Throwable” is the super


class of the class “Exception”
• The “Exception” class in turn is the
super class of other exceptions
Catch and Throw Model
Catch & Throw - Overview

•An Exception-handling model


•According to this model, an
exception is thrown when an error
occurs and it is caught in a block.
“Finally”
block
•Have a look at the diagram

try block

No Exception Exception occurs

finally catch block

finally
•As in the diagram, the “finally”
block is guaranteed to run
whether or not an exception
occurs.
•A “finally” block is used to ensure
that all the clean up work is done
after an exception occurs

•It is used in conjunction with “try”


block.
•The “finally” block contains
statements either to return resources
to the system or print messages. The
statements include :

•Closing a file
•Closing a result set(used in database
programming)
•Closing connection established with
the database.

You might also like