3. AGENDA.
• What is Exception
• What is Exception Handling
• Exception Hierarchy in java
• Difference between Exception & Error.
• Types of Exception
• Types of Built-in- Exception
• Create and Handle Exception..
Exception
Exception Hierarchy
Exception Types.
Checked Exception
Unchecked Exception
Create & handle Exception
4. What is Exception
“An exception is an unexpected event, which occurs during the execution of a program i.e., at run
time, that disrupts the normal flow of the program.”
What is Exception handling:
“The Exception Handling in Java is one of the powerful mechanisms to handle the exception. in
program. so that the normal flow of the program can be maintained.”
Purpose:
The purpose of Exception handling is to detect and report an exception so that proper action can be
taken and prevent the program which is automatically terminate or stop the execution because of that
exception
5. Example:
Statement 1;
Statement 2;
Statement 3; (an exception occurs)
Statement 4;
Statement 5;
Suppose there are 5 Statements in a program and there occurs an exception at
Statement 3 the rest of the code of statement 3,4,5 will not be executed. If we
perform exception handling the rest of the statement will be executed.
If Exception occurs then we have two ways;
pass the exception to its caller (using throws keyword)
handle exception (using try, catch and finally keyword)
6. Keyword Used in Exception Handling are given:
Keywords Description
try
Java try block is used to enclose the code that might
throw an exception. The try block must be followed by
catch or finally.
catch
The "catch" block is used to handle the exception. It
must be followed by try block which means we can't
use catch block alone. catch block may be one or more
than one.
finally
The "finally" block is used to execute the necessary
code of the program. It is executed whether an
exception is handled or not.
Throws
The "throws" keyword is used to pass the exception to
its caller. It is always used with method signature.
Throw
“throw” keyword used to create user
define exception. it is used inside the method.
7. Exception Hierarchy in java
In the Java platform, many classes derive directly from Object, other classes derive from some of those
classes, and so on, forming a hierarchy of classes. Object is the parent class of all the classes in java.
8. Difference Between Exception and Error?
Exception Error
Exception occurs due to our Program. Error occurs due to lack of system resources.
Exception can be handled. Error cannot be handled.
Types:
Checked Exception
Unchecked Exception.
Type:
Unchecked Only.
Examples:
Null Pointer Exception, Array Index Out of Bounds
Exception, Class not Found Exception etc.
Examples:
Virtual Machine Error, Stack Over Flow Error,
Assertion Error, Linkage Error, Out of Memory Error,
etc..
10. Difference Between Built In exception and User Defined Exception?
Built In exception User Defined Exception
Built-in exceptions are the exceptions that are
already available in Java libraries.
Built-in exceptions are the exceptions that are not
available in Java libraries.
Predefined exceptions are raised implicitly
(Automatically) by the runtime system.
Predefined exception must be raised explicitly by
throw keyword.
Types:
Checked Exception
Unchecked exception
Types:
unchecked exception
12. Some exception are given:
File Not Found Exception: This Exception is raised when a file is not accessible or does not exist.
IO Exception: It is thrown when an input-output operation failed or interrupted
NoSuchMethodException: It is thrown when accessing a method that is not found.
Interrupted Exception: It is thrown when a thread is waiting, sleeping, or doing some processing, and it is
interrupted.
Checked Exception
“If compiler checks exception, then it is known as checked exception. These exceptions should be either
pass or handled during compile time. If we do not catch or handle them then the compiler will throw a
compilation error. These are the sub classes of the Exception class.”
15. Unchecked Exception
If The compiler does not check exception it is known as unchecked exception. An unchecked exception (also
known as a runtime exception). Exceptions are checked at runtime.
Unchecked exceptions result from faulty logic that can occur anywhere in a software program. For example, if a
developer invokes a method on a null object, an unchecked NullPointerException occurs.
Arithmetic Exception: It is thrown when an exceptional condition has occurred in an arithmetic operation.
Array Index Out Of Bounds Exception: it occurs if we print out of bound array in program.
Class Not Found Exception: it is occur if we delete the class of java file after comile.
NoSuchMethodException: It is thrown when accessing a method that is not found.
String Index out of Bounds Exception: It is thrown by String class methods to indicate that an index is either
negative or greater than the size of the string
Some exception are given: