Exception
- An exceptional behavior's of a program at the time of its execution is called Exception.
- It is an unexpected or abnormal situation that occurred at runtime.
- Exception initiates the disruption of normal flow of the program.
Types of Exceptions
1. Checked Exception: - It is also known as compile time exceptions. These types of exceptions are checked by the compiler during compilation because these type of exceptions can occur normally. These exceptions cannot be ignored, as a programmer, we should avoid these type of exceptions.
Example:
package quipohouse;
import java.io.File;
import java.io.FileReader;
public class Chacked_Exception
{
public static void main(String[] args)
{
File file = new File("D://file.txt");
FileReader fr = new FileReader(file);
}
}
Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unhandled exception type FileNotFoundException
at Chacked_Exception.main(Chacked_Exception.java:9)
2. Unchecked Exception: These are also called runtime exception. This exception can be solve through various keywords. Logical Errors and Arithmetic errors are comes under this category.
Example:
package quipohouse;
public class unchecked_Example
{
public static void main(String args[])
{
int arr[] = {1, 2, 3, 4};
System.out.println(arr[5]);
}
}
Output:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at Unchecked_Exception.main(Unchecked_Exception.java:7)
Unchecked Exception
1. Arithmetic Exception: This type of exception generally occurs when programming statements are not arithmetically correct. This exception is also called DivideByZeroException.
Example:
package quipohouse;
class QuipoHouse
{
public static void main(String args[])
{
int a = 10,b=0,c;
c = a/b;
System.out.println(c);
}
}
Output:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Main.main(Main.java:6)
2. Null Pointer Exception: This type of exception generally occurs when a variable is pointing nothing or null value.
Example:
package quipohouse;
class QuipoHouse
{
public static void main(String[] args) {
Object ref = null;
ref.toString(); // this will throw a NullPointerException
}
}
Output:
Exception in thread "main" java.lang.NullPointerException
at Tester.main(Tester.java:4)
3. Number Format Exception: This type of exception occurs when a string is converting into number with improper formats.
Example:
package quipohouse;
class QuipoHouse
{
public static void main(String args[])
{
int i= Integer.parseInt("345a");
System.out.println(i);
}
}
Output:
Exception in thread "main" java.lang.NumberFormatException: For input string: "345a"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.base/java.lang.Integer.parseInt(Integer.java:652)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at Main.main(Main.java:5)
4. ArrayIndexedOutOfBounds Exception: This type of exception generally occurs when an array element is accessed with invalid index.
Example:
package quipohouse;
class QuipoHouse
{
public static void main(String args[])
{
int[] arr={1,2,3,4,5};
System.out.println(arr[9]);
}
}
Output:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 9 out of bounds for length 5
at Main.main(Main.java:6)
Exception Handling
- To handle exceptions we use following keyword and group of keywords.
1. try-catch block:
- try block is a block in which the code is written which have a chance to throwing an exception. If the exception occurs by the code which is written in try block then from that line the execution of code in try block stopped.
- catch block is used for handling the exception thrown in try block by writing the exception with its parameters.
Syntax:
try
{
...
...
}
catch(Exception e)
{
...
...
}
Example:
package quipohouse;
public class Try_Catch
{
public static void main(String[] args)
{
try
{
int A[] = {1, 2, 3};
System.out.println(A[10]);
}
catch (Exception e)
{
System.out.println("You made exception in try block.");
}
}
}
Output:
You made exception in try block.
2. Multiple catch block: Multiple catch blocks are used for Handling different types of exception within one try block.
Syntax:
try
{
...
...
}
catch(ArithmeticException e) // Handle arithmatic exception only
{
...
...
}
catch(Exception e) // Handle all exceptions except arithmatic exception
{
...
...
}
Example:
package quipohouse;
public class Try_Multiplecatch
{
public static void main(String[] args)
{
try
{
int A[]=new int[5];
System.out.println(A[6]);
}
catch(ArithmeticException e)
{
System.out.println("ArithmeticException handled here.");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBoundsException handled here.");
}
catch(Exception e)
{
System.out.println("Other exception handled here.");
}
}
}
Output: -
ArrayIndexOutOfBoundsException handled here.