14.exception Handling - Try Catch
14.exception Handling - Try Catch
EXCEPTION
HANDLING
Exception is anything that does not follow the rule
Thrower
Mob
Pho
Catc
Mobile
Phone Thrower
Catcher
Mobile
Phone Thrower
Catcher
Mobile
Phone Thrower
Catcher
Mobile
Phone Thrower
Catcher
Late
Comer
Catcher
ADVANTAGES OF THE APPROACH
Here is a simplified
diagram of the
exception hierarchy
in Java.
• Errors
• Errors represent irrecoverable
conditions such as Java virtual
machine (JVM) running out of
memory, memory leaks, stack
overflow errors, library
incompatibility, infinite recursion,
etc.
• try...catch block
• finally block
• throw and throws keyword
Try
Throw
Catch
Throws
Finally
Keyword Description
try The "try" keyword is used to specify a block where we should
place an exception code. It means we can't use try block alone.
The try block must be followed by either catch or finally.
catch The "catch" block is used to handle the exception. It must be
preceded by try block which means we can't 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.
Try
{
//code that may throw an exception
}
catch(Exception_class_Name ref)
{
}
Try
{
//code that may throw an exception
}
catch(Exception 1)
{
}
catch(Exception 2)
{
}
Syntax of try-finally block
1.try{
2.//code that may throw an exception
3.}finally
4.{
5.}
Types of Java Exceptions
There are mainly two types of exceptions: checked and
unchecked. An error is considered as the unchecked exception.
However, according to Oracle, there are three types of exceptions
namely:
1.Checked Exception
2.Unchecked Exception
3.Error
1) Checked Exception
The classes that directly inherit the Throwable class except RuntimeException and
Error are known as checked exceptions.
For example,
• IOException,
• SQLException,
• Class not found Exception
Checked exceptions are checked at compile-time.
2) Unchecked Exception
The classes that inherit the RuntimeException are known as unchecked
exceptions.
For example,
• ArithmeticException,
• NullPointerException,
• ArrayIndexOutOfBoundsException, etc.
Unchecked exceptions are not checked at compile-time, but they are
checked at runtime.
3) Error
Error is irrecoverable. Some example of errors are OutOfMemoryError,
VirtualMachineError, AssertionError etc.
ArithmeticException
1.String s=null;
2.System.out.println(s.length());//NullPointerException
public class NullPointerExceptionExample {
public static void main(String[] args) {
// Declare a string variable and initialize it to null.
String text = null;
try {
// This line will throw NumberFormatException because "10a" is
not a valid integer.
int result = Integer.parseInt(number);
System.out.println("The number is: " + result);
} catch (NumberFormatException e) {
// This block will catch the NumberFormatException.
System.out.println("NumberFormatException occurred: " +
e.getMessage());
}
}
}
ArrayIndexOutOfBounds Exception
When an array exceeds to it's size, the ArrayIndexOutOfBoundsException occurs.
there may be other reasons to occur ArrayIndexOutOfBoundsException. Consider
the following statements.