Unchecked Exception
Unchecked Exceptions are not checked by compiler during compile time, we will not be forced to handle it, but it is always best to handle the exceptions.
If we don’t handle unchecked exception then compilation error will not come.
All unchecked exceptions are subclasses of RuntimeException class.
Generally, these exceptions occur due to the wrong data provided by user.
As a programmer, we need to judge such cases in advance and need to handle them appropriately.
Example :
- package com.kb.state;
- import java.util.Scanner;
- public class RuntimeExceptionExample {
- public static void main(String args[])
- {
- Scanner scanner = new Scanner(System.in);
- System.out.println("Enter dividend: ");
- int dividend = scanner.nextInt();
- System.out.println("Enter divisor: ");
- int divisor = scanner.nextInt();
- float result = dividend/divisor;
- System.out.println("Result is " +result);
- }
- }
package com.kb.state; import java.util.Scanner; public class RuntimeExceptionExample { public static void main(String args[]) { Scanner scanner = new Scanner(System.in); System.out.println("Enter dividend: "); int dividend = scanner.nextInt(); System.out.println("Enter divisor: "); int divisor = scanner.nextInt(); float result = dividend/divisor; System.out.println("Result is " +result); } }
Above program causes run-time exception if user does not provide proper input
Scenario 1 : User provides proper input
Run above program and give below input
Scenario 2 : User provides improper input
Run above program and give below input
When we compile this program, it will compile successfully but when we run this program , it is throwing an ArithmeticException when user provides bad input
This proves that RuntimeException is not forced by compiler to handle it.
Compiler is not forcing to handle these unchecked exceptions, it does not mean that we should ignore it.
In fact, we need to handle them very carefully.
In case if we don’t handle them then exception stack trace will be displayed to user.
So, we should handle it and show a meaningful message to user as below
- package com.kb.state;
- import java.util.Scanner;
- public class RuntimeExceptionExample {
- public static void main(String args[])
- {
- Scanner scanner = new Scanner(System.in);
- System.out.println("Enter dividend: ");
- int dividend = scanner.nextInt();
- System.out.println("Enter divisor: ");
- int divisor = scanner.nextInt();
- float result = 0;
- try {
- result = dividend/divisor;
- } catch (Exception e) {
- System.out.println("Can not perform division as Divisor is Zero");
- return;
- }
- System.out.println("Result is " +result);
- }
- }
package com.kb.state; import java.util.Scanner; public class RuntimeExceptionExample { public static void main(String args[]) { Scanner scanner = new Scanner(System.in); System.out.println("Enter dividend: "); int dividend = scanner.nextInt(); System.out.println("Enter divisor: "); int divisor = scanner.nextInt(); float result = 0; try { result = dividend/divisor; } catch (Exception e) { System.out.println("Can not perform division as Divisor is Zero"); return; } System.out.println("Result is " +result); } }