Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 11

Exception handling

Exception is an unwanted or unexpected event, which occurs during the execution of a


program, i.e. at run time, that disrupts the normal flow of the program’s instructions.
Exceptions can be caught and handled by the program. When an exception occurs within a
method, it creates an object. This object is called the exception object. It contains
information about the exception, such as the name and description of the exception and the
state of the program when the exception occured
Major reasons why an exception Occurs
1)Invalid user input
2)Device failure
3)Loss of network connection
4)Physical limitations (out of disk memory)
5)Code errors
6)Opening an unavailable file
 differences between Error and Exception:Error: An Error indicates a serious problem that a
reasonable application should not try to catch.
Exception: Exception indicates conditions that a reasonable application might try to catch.
Exception Hierarchy
All exception and error types are subclasses of
class Throwable, which is the base class of the hierarchy
Types of exception
Checked Exception Unchecked Exception
Checked exceptions occur at compile time. Unchecked exceptions occur at runtime.

The compiler checks a checked exception. The compiler does not check these types of exceptions.

These types of exceptions can be handled at the time of compilation. These types of exceptions cannot be a catch or handle at the time of compilation,
because they get generated by the mistakes in the program.

They are the sub-class of the exception class. They are runtime exceptions and hence are not a part of the Exception class.

Here, the JVM needs the exception to catch and handle. Here, the JVM does not require the exception to catch and handle.

•Examples of Checked exceptions:File Not Found Exception •Examples of Unchecked Exceptions:No Such Element Exception
•No Such Field Exception •Undeclared Throwable Exception
•Interrupted Exception •Empty Stack Exception
•No Such Method Exception •Arithmetic Exception
•Io exception •Null Pointer Exception
•Sql exception •Number format exception
Exception handling
try The "try" keyword is used to specify a block where we
should place an exception code. It means we can't use • The Exception Handling in Java is one of the
try block alone. The try block must be followed by
either catch or finally.
powerful mechanism to handle the runtime
errors so that the normal flow of the application can
be maintained.
• Java exception handling is managed via five
catch The "catch" block is used to handle the exception. It
must be preceded by try block which means we can't
keywords:try,catch,throw,throws,finally
use catch block alone. It can be followed by finally
block later.

finally The "finally" block is used to execute the necessary


code of the program. It is executed whether an
exception is handled or not.

throw The "throw" keyword is used to throw an exception.

throws The "throws" keyword is used to declare exceptions. It


specifies that there may occur an exception in the
method. It doesn't throw an exception. It is always used
with method signature.
Mechanism of exception handling
• Default Exception Handling: Whenever inside a method, if an exception has occurred, the method
creates an Object known as an Exception Object and hands it off to the run-time system(JVM). The
exception object contains the name and description of the exception and the current state of the
program where the exception has occurred. Creating the Exception Object and handling it in the run-
time system is called throwing an Exception. There might be a list of the methods that had been
called to get to the method where an exception occurred. This ordered list of the methods is
called Call Stack. Now the following procedure will happen.
• The run-time system searches the call stack to find the method that contains a block of
code that can handle the occurred exception. The block of the code is called
an Exception handler.
• The run-time system starts searching from the method in which the exception
occurred, and proceeds through the call stack in the reverse order in which methods
were called.
• If it finds an appropriate handler, then it passes the occurred exception to it. An
appropriate handler means the type of the exception object thrown matches the type
of the exception object it can handle.
• If the run-time system searches all the methods on the call stack and couldn’t have
found the appropriate handler, then the run-time system handover the Exception
Object to the default exception handler, which is part of the run-time system. This
handler prints the exception information in the following format and terminates the
program abnormally.
 
Try catch block:
Multiple catch
• A try block can be followed by one or more catch blocks. Each catch
block must contain a different exception handler.
• In many cases,more than one exception could be raised by a single
piece of code
• In order to handle such a situation multiple catch clauses,each
catching a different type can be be specified

You might also like