ExceptionHandeling (1)
ExceptionHandeling (1)
Exception is an event that arises during the execution of the program, and it
terminates the program abnormally. It then displays system generated error
message.
Exception Handling
Exception handling is the mechanism to handle the abnormal termination of the
program.
For example :ClassNotFoundException, NumberFormatException,
NullPointerException etc.
Error Exception
Errors are by default unchecked type. Exception can be either checked or unchecked
type.
Errors are generally caused by environment Exceptions are generally caused by application
in which application is running. itself.
1. Checked Exceptions
Checked exceptions are also known as compiled time exception, because such
exceptions occur at compile time.
Java compiler checks if the program contains the checked exception handler or
not at the time of compilation.
All these exceptions are subclass of exception class.
Developer has overall control to handle them.
For example: SQLException, IOException, ClassNotFoundException etc.
import java.io.File;
import java.io.PrintWriter;
public class Checked_Demo
{
public static void main(String args[])
{
File file=new File("E://abc.txt");
PrintWriter pw = new PrintWriter("file");
}
}
Output:
error: FileNotFoundException
2. Unchecked Exceptions
Unchecked exceptions are also known as runtime exception.
These include logic errors or improper use of API.
For example: ArrayIndexOutOfBoundException, NullPointerException,
ArithmeticException.
Output:
Exception in thread "main" java.lang.ArithmeticException: / by zero
There are five keywords used in Java for exception handling. They are:
i. try
ii. catch
iii. throw
iv. throws
v. finally
Syntax
try
{
//code that cause exception;
}
catch(Exception_type e)
{
//exception handling code
}
class TryCatchDemo
{
public static void main(String args[])
{
int a = 30, b = 3, c = 3;
int result1, result2;
try
{
result1 = a / (b - c);
}
catch(ArithmeticException ae)
{
System.out.println("Divided by zero: "+ae);
}
result2 = a / (b + c);
System.out.println("Result2 = "+result2);
}
}
Output :
Divided by zero: java.lang.ArithmeticException: / by zero
Result2 = 5
class ExceptionDemo
{
public static void main(String args[])
{
try
{
int arr[] = new int[5];
arr[2] = 5;
System.out.println("Access element two: " + arr[2]);
arr[7] = 10;
System.out.println("Access element seven: "+ arr[7]);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Exception thrown:" + e);
}
System.out.println("End of the block");
}
}
Output :
Access element two: 5
Exception thrown: java.lang.ArrayIndexOutOfBoundsException: 7
End of the block
Syntax
try
{
// code which generate exception
}
catch(Exception_type1 e1)
{
//exception handling code
}
catch(Exception_type2 e2)
{
//exception handling code
}
Output:
10 5
Result = 2
10 0
Enter second value except zero.
5
Enter at least two numbers.
10 a
Enter only numeric value.
The code present in finally block will always be executed even if try block
generates some exception.
Finally block must be followed by try or catch block.
It is used to execute some important code.
Finally block executes after try and catch block.
Syntax
try
{
// code
}
catch(Exception_type1)
{
// catch block1
}
Catch(Exception_type2)
{
//catch block 2
}
finally
{
//finally block
//always execute
}
Output:
Exception thrown: java.lang.ArrayIndexOutOfBoundsException: 7
First element value: 5
The finally statement is executed
The throw keyword in Java is used to explicitly throw our own exception.
It can be used for both checked and unchecked exception.
Syntax:
throw new Throwable_subclass;
Output:
Inside test()
Inside main(): java.lang.ArithmeticException: Not valid
Throw Throws
It is used to throw own exception. It is used when program does not handle exception
via try block.