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 :

  1. package com.kb.state;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class RuntimeExceptionExample {  
  6.        public static void main(String args[])
  7.        {
  8.     Scanner scanner = new Scanner(System.in);
  9.     System.out.println("Enter dividend: ");
  10.     int dividend = scanner.nextInt();
  11.    
  12.     System.out.println("Enter divisor: ");
  13.     int divisor = scanner.nextInt();
  14.    
  15.         float result = dividend/divisor;
  16.         System.out.println("Result is " +result);
  17.        }
  18.     }
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

  1. package com.kb.state;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class RuntimeExceptionExample {  
  6.        public static void main(String args[])
  7.        {
  8.     Scanner scanner = new Scanner(System.in);
  9.     System.out.println("Enter dividend: ");
  10.     int dividend = scanner.nextInt();
  11.    
  12.     System.out.println("Enter divisor: ");
  13.     int divisor = scanner.nextInt();
  14.     float result = 0;
  15.         try {
  16.             result = dividend/divisor;
  17.         } catch (Exception e) {
  18.             System.out.println("Can not perform division as Divisor is Zero");
  19.             return;
  20.         }
  21.         System.out.println("Result is " +result);
  22.        }
  23.     }
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);
	   }
	}

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on