EXCEPTION HANDLER IN JAVA
Excepiton: In Java, an exception is an event that disrupts the normal flow of the program. It is an object which is thrown at runtime.
Exception hadnling: The Exception Handling in Java is one of the powerful mechanism to handle the runtime errors so that normal flow of the application can be maintained.
Exception Handling is a mechanism to handle runtime errors such as ClassNotFoundException, IOException, SQLException, RemoteException, etc.
Example for exception handling in Java:
public class JavaExceptionExample{
public static void main(String args[])
{
int a = 10;
try
{
int z= a/0;
}
catch(ArithmeticException e)
{
System.out.println(e);}
}
}
Types of Java Exceptions:
- Checked Exception
- Unchecked Exception
1. Checked Exception: The classes which directly inherit Throwable class except RuntimeException and Error are known as checked exceptions e.g. IOException..
Checked exceptions are checked at compile-time.
2. Unchecked Exception: The classes which inherit RuntimeException are known as unchecked exceptions e.g. ArithmeticException, ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not checked at compile-time, but they are checked at runtime.
Java exception keywords:
- try
- catch
- fanally
- throw
- throws
References:
- Sebesta,”Concept of programming Language”, Pearson Edu
- Louden, “Programming Languages: Principles & Practices” , Cengage Learning
- Tucker, “Programming Languages: Principles and paradigms “, Tata McGraw –Hill.
- E Horowitz, “Programming Languages”, 2nd Edition, Addison Wesley