Beruflich Dokumente
Kultur Dokumente
Agenda
What is Exception?
Types Of Exceptions
Custom Exception
Exception
Checked Exceptions.
Unchecked Exceptions
Unchecked Exceptions
try
catch
finally
throw
throws
Try Block
Syntax :
try {
Code having the possibility to raise
exception.
}
Catch block
Syntax :
try {
int number/0; // Code having the possibility
to raise exception.
}
catch(Exception e)
{
System.out.println("Divider number should
not be Zero");
}
Output :
Syntax :
finally
{
Exception handling statements;
}
Example
class SimpleExample{
public static void main(String args[]) {
try{
int data=25/5;
System.out.println(data);
}
catch(NullPointerException e){
System.out.println(e);
}
finally {
System.out.println("finally block is always executed");
}
System.out.println("rest of the code...");
}
}
Output : 5
finally block is always executed
rest of the code...
Throw
Syntax :
if(age<18)
{
throw new ArithmeticException("not valid");
}
Example
class ExceptionDemo{
Syntax :
Output:exception handled
Custom Exception
Syntax :
class ExceptionDemo{
static void validate(int age)throws InvalidAgeException{
if(age<18)
throw new InvalidAgeException("not valid");
else
System.out.println("welcome to vote");
}