Lecture - 13-Exception Handling
Lecture - 13-Exception Handling
2
Exceptions Handling
Difference between Error and Exception
Errors:
• Indicate serious problems and abnormal conditions that most applications should not try to handle.
• Error defines problems that are not expected to be caught under normal circumstances by our program.
• For example:
memory error, hardware error, JVM error etc.
• Errors are typically ignored in code because you can rarely do anything about an error.
• Example: if stack overflow occurs, an error will arise. This type of error is not possible handle in code.
Exceptions:
• Exception is an abnormal condition.
• An exception (or exceptional event) is a problem that arises during the execution of a program.
• In java, exception is an event that disrupts the normal flow of the program.
• It is an object which is thrown at runtime.
What is exception handling?
• Checked Exception
• Unchecked Exception
• Error
1. Checked exceptions
• All exceptions other than Runtime Exceptions are known as Checked
exceptions as the compiler checks them during compilation
• ClassNotFoundException
• IllegalAccessException
• NotSuchFieldException
• EOFExceptionException
2. Unchecked Exceptions
• Runtime Exceptions are also known as Unchecked Exceptions as the
compiler do not check whether the programmer has handled them or
not
• ArithmaticException
• ArrayIndexOutOfBoundException
• NullPointerException
• NegativeArraySizeException
Hierarchy of Java Exception classes
Exception classes
Difference between checked and unchecked
exceptions
• Checked Exception
The classes that extend Throwable class except RuntimeException and Error are
known as checked exceptions e.g.IOException, SQLException etc. Checked
exceptions are checked at compile-time.
• Unchecked Exception
The classes that extend RuntimeException are known as unchecked exceptions
e.g.ArithmeticException,NullPointerException, ArrayIndexOutOfBoundsException
etc. Unchecked exceptions are not checked at compile-time rather they are checked
at runtime.
• Error
Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError
etc.
Common scenarios where exceptions may occur
1. Scenario where ArithmeticException occurs
If you compile and execute the above program you will get exception as shown below.
• try
• catch
• finally
• throw
• throws
try catch in java
Syntax of try catch in java
Problem without exception handling
• Output:
Exception in thread main java.lang.ArithmeticException:/ by zero
Solution by exception handling
• Let's see the solution of above problem by java try-catch block.
try{
int data=50/0;
}
catch (ArithmeticException e)
{
System.out.println(e);
}
• Output:
Exception in thread main java.lang.ArithmeticException:/ by zero
Flow of control in try/catch blocks:
* When exception doesn’t occur
int x = 10;
int y = 10;
try {
int num= x/y;
System.out.println("next-statement: Inside try block");
}
catch (Exception ex)
{
System.out.println("Exception");
}
System.out.println("next-statement: Outside of try-catch");
• Output:
next-statement: Inside try block
next-statement: Outside of try-catch
• In the above example exception didn’t occur in try block so catch block didn’t run.
Flow of control in try/catch blocks:
* When exception occurs
int x = 10;
int y = 0;
try {
int num= x/y;
System.out.println("next-statement: Inside try block");
}
catch (Exception ex)
{
System.out.println("Exception Occured");
}
System.out.println("next-statement: Outside of try-catch");
• Output:
Exception Occurred
next-statement: Outside of try-catch
• Point to note in above example: There are two statements present inside try block. Since exception occurred
because of first statement, the second statement didn’t execute. Hence we can conclude that if an exception
occurs then the rest of the try block doesn’t execute and control passes to catch block.
Multiple catch blocks
• try
{
//Protected code
}
• catch(ExceptionType1 e1)
{
//Catch block
}
• catch(ExceptionType2 e2)
{
//Catch block
}
try
{
//statements that may cause an exception
}
finally
{
//statements to be executed
}
A finally block appears at the end of the catch blocks and has the following
syntax:
try
{
//Protected code
}
catch(ExceptionType1 e1)
{
//Catch block
}
finally
{
//The finally block always executes.
}
Java throw keyword
SELF STUDY
Questions?
• What is the difference between checked and unchecked exceptions ?
• What happens behind the code int data=50/0; ?
• Why use multiple catch block ?
• Is there any possibility when finally block is not executed ?
• What is exception propagation ?
• What is the difference between throw and throws keyword ?
• What are the 4 rules for using exception handling with method
overriding ?
Thank You