0% found this document useful (0 votes)
1 views16 pages

Exception Handling

The document explains exception handling in programming, detailing what exceptions are, how they occur, and the importance of handling them to prevent program termination. It describes the use of try and catch blocks, nested try-catch structures, and the distinction between checked and unchecked exceptions. Additionally, it emphasizes the necessity of closing heavy-weight objects and the role of the finally block in resource management.

Uploaded by

mfgn86rgj7
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)
1 views16 pages

Exception Handling

The document explains exception handling in programming, detailing what exceptions are, how they occur, and the importance of handling them to prevent program termination. It describes the use of try and catch blocks, nested try-catch structures, and the distinction between checked and unchecked exceptions. Additionally, it emphasizes the necessity of closing heavy-weight objects and the role of the finally block in resource management.

Uploaded by

mfgn86rgj7
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/ 16

Exception Handling

Every rule has an exception,


and it's usually remedial
Exception – What it is?

 It is a runtime interruption which makes the program to stop from executing.

 Exception occurs only during runtime and when it occurs, program gets terminated.

 Exception never occurs at compile time.

 Exception is thrown by JVM.


Exception Hierarchy
Exception Handling
When an exception occurs, the program definitely terminates. If the exceptions are
handled, then we can avoid program termination but the program may not give the
expected output.

How to handle?

 By using try and catch blocks

 One or multiple lines of code which possibly gives an exception must be


enclosed in try block followed by the immediate catch block.

 If the exception did not occur in try block, catch block will not get executed.
How to handle?

 catch block gets executed only if exception happens in the try block and if the
exception matches the catch block.

 When exception occurs in the try block, then the control immediately comes out
of try block without executing the remaining lines of code with in the try block
and executes the matching catch block.

 If the matching catch block is not found, then the program gets terminated.

 Once the control comes out of try block, it will not got to try block again.

 There must not be any executable code in between try and catch blocks.

 Handling scenarios must be written within the catch block.


How to handle?

 Exception means, jvm creates object of appropriate exception class and that
object is thrown.

 Reference type used in the catch block must belong to exception category. That
is, it must be directly or indirectly a sub class of throwable.
try with multiple catch
try
{
… math operation …
… save the results to file …
… save the results to a database …
}
catch(ArithmeticException ae)
{
Handling scenarios
}
catch(IOException ioe)
{
Handling scenarios
}
catch(SQLException sqle)
{
Handling scenarios
}
Nested try and catch blocks
try
{
… math operation …
try {
… save the results to file …
}
catch(IOException ioe)
{
Handling scenarios
}

try {
… save the results to a database …
}
catch(SQLException sqle)
{
Handling scenarios
}
}
catch(ArithmeticException ae)
{
Handling scenarios
}
Nested try and catch blocks
try
{
… math operation …
try {
… save the results to file …
}
catch(IOException ioe)
{
Handling scenarios
}

try {
… save the results to a database …
}
catch(SQLException sqle)
{
Handling scenarios
}
}
catch(ArithmeticException ae)
{
Handling scenarios
}
Points to Remember

 We can write try and catch block inside another


 try block
 catch block
 finally block

 If the handling scenario is the same for multiple exceptions, then we write single
catch block.

 If the handling scenario is different for different exceptions, then we write


multiple catch blocks.

 From jdk 1.7 onwards, we can handle multiple exceptions in a single catch block.

 In case of multiple catch blocks, the sequence must always be sub class to super
class.
Heavy weight objects
 Those objects which consumes system or network resources.

 It is very important to close such resources otherwise, the application becomes slow and
creates performance issues.

 Heavy weight object examples:


 Scanner
 File Reader
 File Writer
 Data Base Connection

How to close?
Scanner sc = new Scanner(System.in);
int c = sc.nextInt();
…. remaining lines of code ….
sc.close();
finally block

 finally is a block which is used in exception handling.

 It always gets executed irrespective of whether the exception occurs or not.

 Single try block can have only one finally block.

 Usually costly (heavy weight object) resources are closed inside finally block.
Checked Exception

 Those exception for which the compiler checks at the compile time, whether
the exception is handled by the developer inside the code or not.

 If the exception is not handled, then the compiler gives an error and force
the developer to handle the exception.

 The exception certainly happens only at the runtime but the compiler
checks whether it is handled or not.

 Checked exception classes never extends RuntimeException class.


Checked Exception
Unchecked Exception

 Those exception for which the compiler does not checks at the compile
time, whether the exception is handled by the developer inside the code or
not.

 If the exception is not handled, then the compiler does not give any error
and will not force the developer to handle the exception.

 Unchecked exception classes always extends RuntimeException class.


Unchecked Exception

You might also like