Skip to main content

Exception in Java


     Exception means abnormal condition. When this kind of condition occurs in the program during runtime, it will throw an object in the runtime. This will affect the user experience so, this needs to be handled in various ways.

    On many sites, we have seen messages under the fields. For example, according to the requirement, the age field should accept only numeric values, if the user enters alphabets in it, it will show an error message as "This field should accept only numeric value". Rather than termination of the program, it allows user to process their requirements. Like this example, all exceptions need to be handled to avoid a bad user experience.

Types of Exceptions

There are 3 types of exceptions
  1. Checked Exceptions
  2. Unchecked Exceptions
  3. Errors
Below is the hierarchy of these classes
  • java.lang
  • Object
  • Throwable
    • Errors
      • StackOverflowError
      • VirtualMachineError
      • OutOfMemoryError
    • Exceptions
      • IOException
      • SQLException
      • ClassNotFoundException
      • Runtime exception(Unchecked Exception)
        • ArithmeticException
        • NullPointerException
        • NumberFormatException
        • IndexOutOfBoundException
          • ArrayIndexOutOfBoundException
          • StringIndexOutOfBoundException

1. Checked Exceptions

    This occurs during the compile time of the program. This is an important exception which must be handled by the developer because it blocks the program at the entry point itself. 

Sample Program
-----------------------------------------------------------------------------------------------------------------------
class Exception {
public static void main(String[] args) {
int a = 5;
int b = 0;
System.out.println("Age is :"+(a/b));
}
}
-----------------------------------------------------------------------------------------------------------------------

Output:
It throws "java.lang.ArithmeticException" exception

2. Unchecked Exceptions

    It occurs in the run time of the program. Most of these errors are because of the logical errors made during the development process. Some errors are because of a lack of requirement gathering. 

Sample Program

-----------------------------------------------------------------------------------------------------------------------
import java.util.Scanner;

class Exception {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); 
int age = sc.nextInt(); 
System.out.println("Age is:"+age);
}
}
-----------------------------------------------------------------------------------------------------------------------

Output

Input: 10
Output: Age is:10

Input: abc    
Output: Runtime Exception occurs(java.util.InputMismatchException)

3. Errors

    These are not exceptions, and these will not be handled in anyways. This may be missing any piece of code or any logic mistakes mostly this will be human errors. Both errors and exceptions come under Throwable. 

Comments