Java Unit-5 Exceptions Handling
Java Unit-5 Exceptions Handling
Dr.S.Amarnadh
Assistant Professor, Dept. of Computer Science And Engineering
GITAM School of Technology
Why the air line staff insists on wearing our seat belts?
• You have to fasten your seat belts to protect yourself from mishaps
that can occur during take off and landing.
• Once we know what are the different situations that can disrupt the
flow of execution, we can take preventive measures to overcome
these disruptions.
6
Department of Computer Science and Engineering
Compile-time errors: Examples
Class Error {
Public static void main (string args [ ]) {
system.out.print(“Can you find errors in
me?”)
}
}
class AnotherError {
public void insert( ){
System.out.print(“To insert a text”);
}
}
Department of Computer Science and Engineering 7
Some common compile-time errors
• Missing semicolons.
• Missing (or mismatch of brackets) in classes and methods.
• Misspelling of identifiers or keywords.
• Missing double quotes in strings.
• Use of undeclared variables.
• Incomplete types in assignment / initialization.
•Bad references to objects. and many more ….
Examples:
Java Run-Time Environment throws an object called IllegalArumentType
when a method m(int x, int y) is called as m(1.5, 4);
ArithmeticException
IOException
Exception NullPointerException
RuntimeException
IndexOutOfBoundsException
Many more classes
Object Throwable IllegalArgumentException
Error VirtualMachineError
ClassNotFoundException
ArithmeticException
IOException
Exception NullPointerException
RuntimeException
IndexOutOfBoundsException
Many more classes
Object Throwable IllegalArgumentException
Error VirtualMachineError
ArithmeticException
IOException
Exception NullPointerException
RuntimeException
IndexOutOfBoundsException
Many more classes
Object Throwable IllegalArgumentException
Error VirtualMachineError
16
Department of Computer Science and Engineering
Unchecked Exceptions
ClassNotFoundException
ArithmeticException
IOException
Exception NullPointerException
RuntimeException
IndexOutOfBoundsException
Many more classes
Object Throwable IllegalArgumentException
Error VirtualMachineError
21
Department of Computer Science and Engineering
Exception handling in Java
22
Department of Computer Science and Engineering
Catching Exceptions
Call Stack
method3
method2 method2
23
Department of Computer Science and Engineering
Exception handling in Java
Simple try- try with Multiple exceptions Exception with Throwing own Nested try-catch
catch multiple catch exit code exception block
Multiple Throws/
try with try-catch throw in try- try within
try with a exception with finally another try
multiple block
single catch with one
catch
catch
24
Department of Computer Science and Engineering
Simple try-catch
25
Department of Computer Science and Engineering
Simple try-catch: Example
class DivideZero {
static int anyFunction ( int x, int y )
{
int a = x/y;
return (a);
}
Public static void main (String args [] )
{
int result = anyFunction (25, 0) ;// Exception occurs here
as y = 0
System.out.println ( “ Result : ” + result );
}
}
26
Department of Computer Science and Engineering
class DivideZero {
Simple try-catch: Example
static int anyFunction (int x, int y )
{
try {
int a = x/y;
return(a);
}
catch (ArithmeticException e) {
System.out.println ( “a = x/y is bypassed… Enter y as non- zero” );
}
}
public static void main (String args[ ] {
int a,b, result;
system.out.print(“Enter any two integers : “);
a = System.in.read( );
b = System.in.read( );
result = any Function (a, b);
System.out.println ( “Result : ” + result);
}
} 27
Department of Computer Science and Engineering
try-catch: Making program robust
28
Department of Computer Science and Engineering
try with Multiple catch:
29
Department of Computer Science and Engineering
try with Multiple catch:
30
Department of Computer Science and Engineering
Multiple errors with single catch
31
Department of Computer Science and Engineering
finally in try-catch
32
Department of Computer Science and Engineering
finally in try-catch
33
Department of Computer Science and Engineering
throw in try-catch
34
Department of Computer Science and Engineering
throw in try-catch: Example
35
Department of Computer Science and Engineering
throws in try-catch
36
Department of Computer Science and Engineering
throws in try-catch
37
Department of Computer Science and Engineering
Nested try-catch
38
Department of Computer Science and Engineering
Declaring your own exception
All exceptions must be a child of Throwable.
• If you want to write a checked exception that is automatically enforced by the
Handle or
Declare Rule, you need to extend the Exception class.
• If you want to write a runtime exception, you need to extend the
RuntimeException class
Syntax
39
Department of Computer Science and Engineering
Declaring your own exception
40
Department of Computer Science and Engineering
Thank You