Menu

How to throw NullPointerException in a Java program

Exception Handling in Java program.

Throwable interface has three class.
  1. Error
  2. Runtime Exception
  3. Exception

There are two types of exception in Java.

Checked: Which can be handle using try and catch block

  • Exception class comes under the checked exception which we can handle.
    • NullPointerException
    • DivideByZeroException
    • IndexOutOfBoundException
    • ClassCastException

Unchecked : Which cannot be handle.

  • Error and Runtime Exception come under the unchecked exception which we can't handle.


Below is a example program:

package rashid;

public class testException {

public static void main(String[] args) {
String var1=null;

System.out.println(var1.length());
}

}

Output:
Exception in thread "main" java.lang.NullPointerException
at rashid.testException.main(testException.java:8)


No comments:

Post a Comment