0% found this document useful (0 votes)
2 views

Lecture 14 Oops With Java

The document explains exception handling in Java, detailing how control flow changes when exceptions occur and how the JVM reacts by searching for appropriate handlers. It outlines the use of keywords such as try, catch, finally, throw, and throws, and provides syntax examples for try-catch and try-finally blocks. Additionally, it describes the JVM's default handling of unhandled exceptions, including printing descriptions and stack traces before program termination.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture 14 Oops With Java

The document explains exception handling in Java, detailing how control flow changes when exceptions occur and how the JVM reacts by searching for appropriate handlers. It outlines the use of keywords such as try, catch, finally, throw, and throws, and provides syntax examples for try-catch and try-finally blocks. Additionally, it describes the JVM's default handling of unhandled exceptions, including printing descriptions and stack traces before program termination.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

OOPS with JAVA

Exception Handling in Java


Understanding Control Flow, JVM Reactions, and Use of try-
catch

Assistant Professor
Control Flow in Exceptions

• Control flow in exceptions refers to how a program's


execution path changes when an exception is
encountered.
• When an exceptional condition occurs during the
execution of a program, such as division by zero or
attempting to access an index beyond the bounds of an
array, an exception is thrown.
• This interrupts the normal execution flow and transfers
control to an exception handler.
• The handler can then take appropriate action, such as
logging the error, displaying an error message to the user,
or attempting to recover from the exceptional condition
JVM Reaction to Exceptions
JVM Reaction to Exceptions

The JVM reacts to exceptions based on predefined rules.


•When an exception occurs within a method, the JVM searches
for an appropriate exception handler.
•If the method where the exception occurred contains a try-
catch block that matches the thrown exception, the control is
transferred to the corresponding catch block.
• If there's no suitable catch block in the current method, the
JVM proceeds to unwind the method call stack.
•It continues searching through the call stack until it finds a
method that contains an appropriate exception handler.
•If the exception remains unhandled after the call stack is
unwound, the JVM may terminate the program.
•Termination of the program might involve printing a stack trace,
which provides information about the location and nature of the
exception.
Java Exception Handling Keywords
There are 5 keywords used in Java exception
handling.
• try
• catch
• finally
• throw
• throws
Java try block
• Java try block is used to enclose the code that
might throw an exception. It must be used
within the method.
• Java try block must be followed by either catch
or finally block.
Syntax of java try-catch
try{
//code that may throw exception
}catch(Exception_class_Name ref){}
Syntax of try-finally block
try{
//code that may throw exception
}finally{}

• Java catch block is used to handle the


Exception. It must be used after the try block
only.
• You can use multiple catch block with a single
try.
Internal working of java try-catch block
The JVM firstly checks whether the exception is
handled or not. If exception is not handled,
JVM provides a default exception handler that
performs the following tasks:
• Prints out exception description.
• Prints the stack trace (Hierarchy of methods
where the exception occurred).
• Causes the program to terminate.
References …
• https://www.javatpoint.com/exception-handling-in-java
Disclaimer (Calibri, font size 40-44)
Thank You

You might also like