Lecture 12
Lecture 12
❑ After execution:
Output:
Exception: java.lang.ArithmeticException: / by zero
Syntax of Multiple Catch Clauses
try {
// Code which might throw an exception
}
catch(FileNotFoundException x) {
// code to handle a FileNotFound exception
}
catch(IOException x) {
// code to handle any other I/O exceptions
}
catch(Exception x) {
// Code to catch any other type of exception
}
Syntax of Nested Try Statements
try {
try {
// Code which might throw an exception
}
catch(ArithmeticException ex1) {
// code to handle a ArithmeticException exception
}
}
catch(FileNotFoundException ex2) {
// code to handle a FileNotFound exception
}
Syntax of try-catch-finally
try {
// Code which might throw an exception
}
catch(IOException x) {
// code to handle any other I/O exceptions
}
catch(Exception x) {
// Code to catch any other type of exception
}
finally {
// This code is ALWAYS executed
}
Syntax of throw
❑ So far, we have only been catching exceptions that are thrown by the Java run-time system. However, it
is possible for our program to throw an exception explicitly, using the throw statement.
❑ The general form of throw is shown here:
Common Exceptions:-
❑ ArithmeticException:
❑ When we try to divide some value by 0, ArithmeticException occurs.
❑ For example: 42/0, it will throw an ArithmeticException.
Try {…….}
catch (ArithmeticException e) {
System.out.println("Exception: " + e);
}
❑ InputMismatchException:
❑ Occurs when an invalid input is given in a program.
❑ For example: it occurs when we give a character as input instead of an integer.
❑ NumberFormatException:
❑ Occurs when we try to convert a String to an integer or double, but the string does not contain
any numeric character.
❑ NullPointerException:
❑ Occurs when we try work (access/use/check) on a null value.
❑ ArrayIndexOutOfBoundException:
❑ Occurs when we try to access to an index position larger than the maximum index value of the
array index.
❑ For example, if the size of an array is 5 then, the maximum value of index is 4.
ArrayIndexOutOfBoundException will be thrown if we try to access an index position greater
than 4.
❑ IOException
❑ ClassnotFoundException
❑ NoClassDefFoundError
❑ IlligalStateExceptation
❑ SQLException
❑ FileNotFoundException
Syntax of throws
❑ If a method can cause an exception that it does not handle (we have seen it in throw example), it must
specify this behavior so that callers of the method can guard themselves against that exception.
❑ This is the general form of a method declaration that includes a throws clause:
Exception declearation:-
Public void calculate (int a, int b) {
If (a<0) {
Through new IllegalArgumentException(“Argument a can not be negative”);
}
if(b<0) {
Through new IllegalArgumentException(“Argument b can not be negative”);
}
}
Example of throw
Method throwException
Exception handled in method throwException
Finally executed in throwException
Exception handled in main
Method doesNotThrowException
Finally executed in doesNotThrowException
End of method doesNotThrowException
Error vs Exception
Checked vs Unchecked
❑ Checked Exception (Compile Time Exception)
❑ A checked exception is an exception that occurs at the compile time. They are also called as
compile time exceptions. These exceptions cannot simply be ignored at the time of compilation.
❑ If your code invokes a method which is defined to throw checked exception, your code MUST
provide a catch handler.
❑ The compiler generates an error if the appropriate catch handler is not present.
❑ Checked exceptions are subclassed from Exception class.
❑ Unchecked Exception (Run Time Exception)
❑ An unchecked exception is an exception that occurs at the time of program execution. They are
also called as Runtime Exceptions.
❑ Runtime exceptions are ignored at the time of compilation.
❑ If an unchecked exception is not caught, it will go to the default catch-all handler for the
application.
❑ All Unchecked exceptions are subclassed from RuntimeException.
What is User Defined Exception?
❑ Although Java’s built-in exceptions handle most common errors, but sometimes it is necessary create
our own exception. These are user defined exception.
How to Define User Defined Exception?
❑ To define your own exception you must do the following:
❑ Create an exception class to hold the exception data.
❑ Your exception class must subclass "Exception" or another exception class
Note: to create unchecked exceptions, subclass the RuntimeException class.
❑ Minimally, your exception class should provide a constructor which takes the exception
description as its argument.
Example of User Defined Exception