Based on our programming requirement some times we have to create our own exception, which are nothing but “Customized Exception”.

Example:

package com.java2learn.exception;
public class CustomExceptionDemo {

	public static void main(String[] args) {

		double balance = Integer.parseInt(args[0]);
		System.out.println(balance);
		if (balance < 5000.0) {
			throw new InsufficientFunds("Minimum balance should be 5000");
		}
	}
}

class InsufficientFunds extends RuntimeException {
	public InsufficientFunds(String exception) {
		super(exception);
	}
}

java CustomExceptionDemo 2000

Output:
Exception in thread “main” com.java2learn.exception.InsufficientFunds: Minimum balance should be 5000
at com.java2learn.exception.CustomExceptionDemo.main(CustomExceptionDemo.java:9)

Note: It is recommended to define customized exceptions as unchecked. i.e our custom exceptions class should extends RuntimeException either directly or indirectly.

Top 10 Exceptions

All Exceptions are divided into two categories
JVM Exceptions:
Raised automatically by the JVM when ever a particular condition occurs.
Example: ArithmeticException, NullPointerException.
ProgramaticExceptions: These are raised programmatically because of programmers code or API’s developers Code.
Example: IllegalArgumentException, NumberFormatException.

  1. NullPointerException:
    It is the direct child class of RuntimeException and it is unchecked. Thrown automatically by the JVM when ever we are performing any operation on null.

    Example: String s = null
    System.out.println(s.length());// NullPointerException.

  2. StackOverFlowError: It is the child class of Error and it is unchecked. Raised automatically by the JVM when ever we are performing recursive method invocation.

    Example:

    package com.java2learn.exception;
    public class Test {
    
    	
    	public static void main(String[] args) {
    	
    		m1();
    	}
    	
    	public static void m1(){
    		m1();
    	}
    
    }
    

    Output:
    Exception in thread “main” java.lang.StackOverflowError
    at com.java2learn.exception.Test.m1(Test.java:12)
    at com.java2learn.exception.Test.m1(Test.java:12)

  3. ArrayIndexOutOfBoundsException: It is the child class of RuntimeException and it is unchecked thrown automatically by the JVM when ever we are accessing an array element with invalid int index.(Out of range index)

    Example:
    int [] a = {10, 20, 30};
    System.out.println(a[0]);
    System.out.println(a[20]);// ArrayIndexOutOfBoundsException

  4. ClassCastException: It is the child class of RuntimeException and it is unchecked. Thrown automatically by the JVM when ever we are trying to typecast parent class object to the child type.

    Example:1
    String s = “java”;
    Object o = (Object)s
    Example:2
    Object o = new Object();
    String s = (String)o;
    RuntimeException: ClassCastException.

  5. NoClassDefFoundError: It is the child class of Error and it is unchecked. Thrown automatically by the JVM if the required .class file is not available.

    Ex: java dummy
    If dummy.class file is not available we will get NoClassDefFoundError.

  6. ExceptionInInitializerError: It is child class of Error and it is unchecked. Raised automatically by the JVM when ever an Exception occur during initialization of static variables or execution of static blocks.

    Example:1

    class Test{
    static int i = 10/0;
    }
    

    Output:
    java.lang.ExceptionInInitializerError
    Caused by: java.lang.ArithmeticException: / by zero
    at com.java2learn.exception.Test.(Test.java:5)
    Exception in thread “main”

    Example2:

    package com.java2learn.exception;
    public class Test {
    
        static{
        	String s=null;
        	System.out.println(s.length());
        }
    }
    

    Output:
    java.lang.ExceptionInInitializerError
    Caused by: java.lang.NullPointerException
    at com.java2learn.exception.Test.(Test.java:7)
    Exception in thread “main”

  7. IllegalArgumentException: It is the child class of RuntimeException and it is unchecked thrown explicitly by the programmer or API developer when ever we are invoking a method with inappropriate or invalid argument.

    Example:
    The valid range of Thread priority is 1 – 10 , if we are trying to invoke setPriority() method with 100 as argument we will get IllegalArgumentException.

  8. NumberFormatException: It is the child class of Illegal Argument and it is unchecked. Thrown programmatically when ever we are attempting to convert String to Number type but the String is not formatted properly

    Example:

    package com.java2learn.exception;
    public class Test {
    
    	public static void main(String[] args) {
    		String s = "ten";
    		int i = Integer.parseInt(s);
    	}
    }
    

    Output:
    Exception in thread “main” java.lang.NumberFormatException: For input string: “ten”
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:447)
    at java.lang.Integer.parseInt(Integer.java:497)
    at com.java2learn.exception.Test.main(Test.java:7)

  9. IlleaglStateExcepiton: It is the child class of RuntimeException and it is unchecked. Thrown programmatically to indicate that a method has invoked at an inappropriate time.
    After invalidating a session object we are not allowed to call any method on that object other wise IllegalStateException.
    After comitting the response we are not allowed to redirect or forward otherwise IlleagalStateException
    After starting a thread we are not allowed to start the same thread once again other wise IlleagalStateException.
    Example:
    MyThread t = new MyThread();
    t.start();
    t.start(); // IllegalThreadStateException.
  10. AssertionError: It is the child class of Error and it is unchecked thrown programmatically to indicate Assertion fails.

    Example:
    assert(false);// AssertionError