1. ADHIYAMAAN COLLEGE OF ENGINEERING (AUTONOMOUS)
DEPARTMENT OF INFORMATION TECHNOLOGY
Mrs.S.Sujatha ,ME,
Assistant Professor,
Dept of Information Technology,
Adhiyamaan College of Engineering, Hosur
3. EXCEPTION
• An exception is an unwanted or unexpected
event, which occurs during the execution of a
program i.e at run time, that disrupts the
normal flow of the program’s instructions.
• There are two type of exception
– Checked exception
– Unchecked Exception
4. CHECKED EXCEPTION
• All exceptions other than Runtime Exceptions
are known as Checked exceptions as the
compiler checks them during compilation to
see whether the programmer has handled
them or not.
• If these exceptions are not handled/declared
in the program, you will get compilation error.
• For example, SQLException, IOException,
ClassNotFoundException etc.
5. EXAMPLE
Class file
{
public static void main(String args[])
{
file f=new file(“e://file.txt”);
fileReader fr =new fileReader(file);
}
}
o/p:
error
6. UNCHECKED EXCEPTION
• Runtime Exceptions are also known as Unchecked
Exceptions.
• These exceptions are not checked at compile-
time so compiler does not check whether the
programmer has handled them or not but it’s the
responsibility of the programmer to handle these
exceptions and provide a safe exit.
• For example, ArithmeticException,
NullPointerException,
ArrayIndexOutOfBoundsException etc.
8. HANDLING EXCEPTION
• Find the problem(hit the exception)
• Inform the error has occur(throw)
• Recevied error(catch)
• Take action
• Close all file and threads
SYNTAX
Try
{
Body of try block;
}
Catch(Exception e)
{
Handle statement;
}
9. Using try -catch
• The try block contains set of statements where an
exception can occur.
• A try block is always followed by a catch block, which
handles the exception that occurs in associated try
block.
• A try block must be followed by catch blocks or finally
block or both.
SYNTAX
try{
//statements that may cause an exception
}
10. catch
• A catch block is where you handle the exceptions, this
block must follow the try block. A single try block can
have several catch blocks associated with it
Try
{
//statements that may cause an exception
}
catch (exception(type) e(object))
{
//error handling code
}
11. EXAMPLE
Class Example1
{
public static void main(String args[])
{
int num1, num2;
try
{
num1 = 0;
num2 = 62 / num1;
System.out.println(num2);
System.out.println("Hey I'm at the end of try block");
}
catch (ArithmeticException e)
{
System.out.println("You should not divide a number by zero");
}
}
}
12. MULTIPLE CATCH
• A try block can be followed by one or more catch blocks. Each catch block must
contain a different exception handler.
• SYNTAX
try {
// Protected code
}
catch (ExceptionType1 e1)
{ // Catch block
}
catch (ExceptionType2 e2)
{
// Catch block
}
catch (ExceptionType3 e3) {
// Catch block
}
13. EXAMPLE
public class MultipleCatchBlock1 {
public static void main(String[] args) {
try{
int a[]=new int[5];
a[6]=30/2;
}
catch(ArithmeticException e)
{
System.out.println("Arithmetic Exception occurs");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBounds Exception occurs");
}
catch(Exception e)
{
System.out.println("Parent Exception occurs");
}
}
}
14. Throw vs throws
• Throws clause is used to declare an exception, which
means it works similar to the try-catch block. On the other
hand throw keyword is used to throw an exception
explicitly.
• If we see syntax wise than throw is followed by an instance
of Exception class and throws is followed by exception class
names.
• Throw keyword is used in the method body to throw an
exception, while throws is used in method signature to
declare the exceptions that can occur in the statements
present in the method.
• throw one exception at a time but you can handle multiple
exceptions by declaring them using throws keyword.
15. Throw Example
public class Example1
{
void checkAge(int age)
{
if(age<18)
throw new ArithmeticException("Not Eligible for voting");
else System.out.println("Eligible for voting");
}
public static void main(String args[])
{
Example1 obj = new Example1();
obj.checkAge(13);
System.out.println("End Of Program");
} }
16. Throws Example
public class Example1
{
int division(int a, int b) throws ArithmeticException
{
int t = a/b;
return t;
}
public static void main(String args[])
{
Example1 obj = new Example1();
try
{
System.out.println(obj.division(15,0));
}
catch(ArithmeticException e)
{
System.out.println("You shouldn't divide number by zero");
}
}
}
17. Finally class
• The finally statement lets you execute code, after try...catch
public class Main
{
public static void main(String[] args)
{
try
{
int[] myNumbers = {1, 2, 3}; System.out.println(myNumbers[10]);
}
catch (Exception e)
{
System.out.println("Something went wrong.");
}
finally
{
System.out.println("The 'try catch' is finished."); } } }
19. • User defined exaception
– Extend the exception class to create user defined
exception.
• Class myexception extend exception
– Intialize user defined exception class by creating
constructor
• Myexception()
{
}
– To raise a exception create object
• Myexception obj=new myexception()
20. example
class InvalidAgeException extends Exception{
InvalidAgeException(String s){
super(s);
}
}
class TestCustomException1{
static void validate(int age)throws InvalidAgeException{
if(age<18)
throw new InvalidAgeException("not valid");
else
System.out.println("welcome to vote");
}
public static void main(String args[]){
try{
validate(13);
}catch(Exception m){System.out.println("Exception occured: "+m);}
System.out.println("rest of the code...");
}
}