You are on page 1of 24

Exception Handling

Exception Handling

• Event that disrupts the normal flow of the program


• Exception handling is a mechanism to handle runtime errors
• Runtime errors leads system failure
• So java supports exception handling
Exception Handling
1. try

2. catch

3. throw

4. throws

5. finally

• More robust and make easier to debug your program


Example 1
General Form of Exception Handling Block

The finally block will execute whether or not an exception is thrown.


Example 2
Multiple Catch Block
Finally Block
Nested try
Exception Types
throw

syntax
throw exception;
throws

return_type method_name() throws exception_class_name{


//method code
}
Errors vs Exception
Errors Exception

Impossible to recover Possible to recover

Unchecked type Checked or unchecked

Happen at run time Happen runtime and compile time

Caused by environment on which application is running Caused by application


• Only Throwable objects can be used with the exception-handling mechanism
• Class Exception and its subclasses represent exceptional situations that can
occur in a Java program and that can be caught by the application.

• Error and its subclasses represent abnormal situations that happen in the
JVM. Most Errors happen infrequently and should not be caught by
applications—it’s usually not possible for applications to recover from Errors
Used by Java run time system
Exception Hierarchy handles the error
Creates exception and hands it
Object to the run time system name
and description, current state
of the program
Throwable Creating the exception object
and handling it to the run time
system is called throwing the
Errors exception
Exceptions

Checked Virtual Machine


Exceptions Errors

Unchecked
Assertion Errors
Exceptions
Checked vs Unchecked

Checked Unchecked
1. Checked by the compiler at 1. Occurs at runtime. Also
compile time called as runtime exceptions.
2. Cannot be ignored, the Built in exceptions in java
programmer should handle 2. Ignored at the compile time
these exceptions Example:Divide by zero
Example: IO exceptions
JAVA Exception Hierarchy

• a small portion of the


inheritance hierarchy

• extend this hierarchy with


your own exception
classes
JAVA Built in Exception
Creating Your Own Exception Subclasses

• create your own exception types to handle situations specific to your


applications.
• define a subclass of Exception
• The Exception class does not define any methods of its own.
• It inherit methods provided by Throwable.
• Thus, all exceptions, including those that you create, have the methods
defined by Throwable available to them.
Methods of Throwable class
Constructors of Exception
• Exception defines four public constructors.
• two are
• Exception( )
• Exception(String msg)
• Two support chained exceptions
• Throwable(Throwable causeExc)
• Throwable(String msg, Throwable causeExc)
Stack Unwinding and Obtaining Information
from
an Exception Object
• When an exception is thrown but not caught in a particular scope, the method-call
stack is “unwound,” and an attempt is made to catch the exception in the next outer
try block. This process is called stack unwinding.
• Unwinding the method-call stack means that the method in which the exception was
not caught terminates, all local variables in that method go out of scope and control
returns to the statement that originally invoked that method.
• If a try block encloses that statement, an attempt is made to catch the exception.
• If a try block does not enclose that statement or if the exception is not caught, stack
unwinding occurs again.
Difference between throw and throws

You might also like