Java Exception – Explore Checked & Unchecked Exception With Examples

Free Java courses with 37 real-time projects - Learn Java

In java, while writing a program a programmer might come across certain difficulties or unusual scenarios. It can be a problem with the program syntax or it can be difficulties in getting the desired output. Whatever the case may be, it is important to solve these issues to make the program give a favourable output. In this article, we will take a look at these unusual scenarios and how to handle them in the program. We will learn about Java exception and its types.

What are Exceptions in java?

We know exceptions are abnormal conditions, which are not a part of our day to day life. In java programming, exceptions are events that hamper the regular flow of a program. It is a kind of object that is thrown during runtime.

Cause of Exception in Java:

An exception can occur due to various reasons. Some common causes of exceptions are:

  • by the user during an input operation.
  • In syntax by the programmer.
  • due to a lack of resources, like insufficient memory.

Categories of exceptions in java:

In java, there are two categories of exceptions:

1. JVM exceptions: These are exceptions and errors that are thrown by the JVM only. Examples: NullPointerException, ArrayIndexOutOfBoundsException, ClassCastException.

2. Programmatic Exceptions: These are exceptions that the application or the API programmers throw explicitly. Examples: IllegalArgumentException, IllegalStateException.

Hierarchy of Java Exceptions:

In java, the exceptions are present in the java.lang.Throwable class. Inheriting the class are two subclasses Exception and Error, which are further inherited by other subclasses. The hierarchy of the Throwable class is:

 

hierarchy of java exceptions

Types of Java Exceptions:

There are mainly two types of exceptions Checked and Unchecked Exceptions, but according to Oracle there are actually three types of exceptions:
1. Checked Exception
2. Unchecked Exception
3. Error

Error is also a type of unchecked exception.

Difference Between Java Checked Exceptions, Unchecked Exceptions and Errors:

1. Checked Exception in Java: The exception classes which directly inherit from the Throwable class with an exception of RuntimeException and Error are checked exceptions. They are checked during compilation time. For example: IOException, SQLException, ClassnotFound, etc.

2. Unchecked Exception in Java: The exception classes inheriting the RuntimeException class are unchecked exceptions. They are not checked at compilation time but during runtime. For example: ArithmeticException, IndexOutOfBOundException, etc.

3. Error in Java: The exceptions which are irrecoverable are known as errors. They are also checked at runtime. For Example: OutOfMemoryError, VirtualMachineError, etc.

How JVM Handles an Exception(Default Exception Handling):

If an exception is encountered inside the method, it creates an object called the Exception Object. The object is then transferred to the run-time system or JVM. The object contains all the information of the exception, starting from name and description of the exception, and the current state of the exception. Various methods might be called where the exception occurred, the ordered list of these methods is known as Call Stack.

After this, the following will happen:

  • The JVM searches the call stack to look for the method that contains the code to handle the occurred exception, this block of code is known as an Exception handler.
  • Now the JVM looks for the method where the exception took place.
  • If the JVM finds an appropriate handler, it passes the exception method to the handler.
  • If the JVM is unable to find an appropriate handler, it sends the object to the default exception handler, which prints a default message and terminates the abnormality.

Code to explain Exception handling by JVM:

package com.DataFlair.exceptions;
public class exception
{
    public static void main(String args[]){
          
        String str = null;
        System.out.println(str.length());
          
    }
}

The output of the above code:

java.lang.NullPointerException
at com.DataFlair.exceptions.exception.main(exception.java:7)
at com.DataFlair.exceptions.__SHELL0.run(__SHELL0.java:6)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at bluej.runtime.ExecServer$3.lambda$run$0(ExecServer.java:849)
at bluej.runtime.ExecServer.runOnTargetThread(ExecServer.java:964)
at bluej.runtime.ExecServer$3.run(ExecServer.java:846)

How Programmer handles an exception(Customized Exception Handling):

Programmers can handle exceptions using the five keywords: try, catch, throw, throws and finally. The code that may contain exceptions is passed through the try block which tries the code for exceptions, the exception is then thrown to the catch block which handles the exception. After that, the finally block is used, which runs whatsoever. The throws and throw keyword is used to give a customized exception.

The proper use of try-catch and finally block is given below in this article.

The common exception scenarios in Java:

1. SQLException: Using invalid SQL queries in the java program might lead to this kind of exception.

2. IOException: Whenever the JVM fails to open the IO stream, this exception is thrown by the JVM.

3. ClassNotFoundException: If we try to access a class that does not exist or has been deleted from the path, the JVM throws this error.

4. ArithmeticException: Dividing a number by zero will give an ArithmeticException, as we all know anything divided by zero is infinite.

Code to illustrate ArithmeticException:

package com.DataFlair.exceptions;
public class exception
{
     public static void main(String args[])
  {
    int exp = 50/0;
    System.out.println("This is an Arithmetic Exception: " + exp);
  }
}

The output of the above Code:

java.lang.ArithmeticException: / by zero
at com.DataFlair.exceptions.exception.main(exception.java:6)
at com.DataFlair.exceptions.__SHELL0.run(__SHELL0.java:6)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at bluej.runtime.ExecServer$3.lambda$run$0(ExecServer.java:849)
at bluej.runtime.ExecServer.runOnTargetThread(ExecServer.java:964)
at bluej.runtime.ExecServer$3.run(ExecServer.java:846)

5. NullPointerException: Trying to find the length of a null string is an example of this kind of exception. We all know that null doesn’t have a physical memory assigned to it, thus it doesn’t have a length. So, it will give a NullPointerException.

Code to explain NullPointerException:

package com.DataFlair.exceptions;
public class exception
{
     public static void main(String args[])
    {
        String str= null;
        System.out.println(str.length());
     }
}

The output of the above code:

java.lang.NullPointerException
at com.DataFlair.exceptions.exception.main(exception.java:7)
at com.DataFlair.exceptions.__SHELL1.run(__SHELL1.java:6)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at bluej.runtime.ExecServer$3.lambda$run$0(ExecServer.java:849)
at bluej.runtime.ExecServer.runOnTargetThread(ExecServer.java:964)
at bluej.runtime.ExecServer$3.run(ExecServer.java:846)

6. NumberFormatException: If we declare a variable as a string with multiple characters and try to convert it into a digit, it will give a NumberFormatException.

Code to Explain NumberFormatException:

package com.DataFlair.exceptions;
public class exception
{
     public static void main(String args[])
    {
        String str= "DataFlair";
        int num=Integer.parseInt(str);
        System.out.println(num);
     }
}

The output of the above code:

java.lang.NumberFormatException: For input string: “DataFlair”
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.base/java.lang.Integer.parseInt(Integer.java:652)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at com.DataFlair.exceptions.exception.main(exception.java:7)
at com.DataFlair.exceptions.__SHELL2.run(__SHELL2.java:6)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at bluej.runtime.ExecServer$3.lambda$run$0(ExecServer.java:849)
at bluej.runtime.ExecServer.runOnTargetThread(ExecServer.java:964)
at bluej.runtime.ExecServer$3.run(ExecServer.java:846)

7. ArrayIndexOutOfBOundException: This is the most common exception that occurs in a program. Programmers often forget the size of the array and call for an index that is larger than the size of the array, causing ArrayIndexOutOfBoundException.

Code to Explain ArrayIndexOutOfBOundException:

package com.DataFlair.exceptions;
public class exception
{
     public static void main(String args[])
    {
        int num[]=new int[50];
        num[100]=1000;
        System.out.println(num[100]);
     }
}

The output of the above code:

java.lang.ArrayIndexOutOfBoundsException: Index 100 out of bounds for length 50
at com.DataFlair.exceptions.exception.main(exception.java:7)
at com.DataFlair.exceptions.__SHELL3.run(__SHELL3.java:6)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at bluej.runtime.ExecServer$3.lambda$run$0(ExecServer.java:849)
at bluej.runtime.ExecServer.runOnTargetThread(ExecServer.java:964)
at bluej.runtime.ExecServer$3.run(ExecServer.java:846)

8. IllegalArgumentException: IllegalArgument Exception is thrown when we pass an incorrect argument into a method.

Code to explain IllegalArgumentException:

package com.DataFlair.exceptions;
public class exception
{
     public static void main(String[] args)
    {
        Thread t1 = new Thread(new Runnable() {
            public void run()
            {
                try {
                    Thread.sleep(-10);
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("DataFlair");
            }
        });
        t1.setName("Test Thread");
        t1.start();
    }
}

The output of the above code:

Exception in thread “Test Thread” java.lang.IllegalArgumentException: timeout value is negative
at java.base/java.lang.Thread.sleep(Native Method)
at com.DataFlair.exceptions.exception$1.run(exception.java:10)
at java.base/java.lang.Thread.run(Thread.java:834)

9. IllegalStateException: If we call a method at the wrong time, we get the IllegalStateException. It means that the state of the environment does not meet the operation.

Code to understand IllegalStateException:

package com.DataFlair.exceptions;
import java.util.ArrayList;
import java.util.ListIterator;
public class exception
{
     public static void main(String args[]) {
      ArrayList<String> list = new ArrayList<String>();
      list.add("Data");
      list.add("Flair");
      ListIterator<String> it = list.listIterator();
    it.remove();//Removing element without moving the first position.
   }
}

The output of the above code:

java.lang.IllegalStateException
at java.base/java.util.ArrayList$Itr.remove(ArrayList.java:1009)
at com.DataFlair.exceptions.exception.main(exception.java:11)
at com.DataFlair.exceptions.__SHELL0.run(__SHELL0.java:6)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at bluej.runtime.ExecServer$3.lambda$run$0(ExecServer.java:849)
at bluej.runtime.ExecServer.runOnTargetThread(ExecServer.java:964)
at bluej.runtime.ExecServer$3.run(ExecServer.java:846)

Exception Handling in Java:

In java, the exceptions can be handled inside the program itself using exception handling. Exception handling can be used to handle runtime exceptions such as IOExceptions, ArithmeticExceptions, etc.

Advantages of using Java Exception Handling:

Using exception handling we can maintain the regular flow of the program. We have seen earlier that exceptions disrupt the regular flow of a program. Thus exception handling skips the exception, without hampering the flow of the program.

Important Keywords Related to Java Exception Handling:

1. try: The try block is the block where the block of code that is needed to be checked for exceptions is placed. The try block is followed by a catch or finally block, it cannot stand alone.

2. catch: Using the catch block we can catch the exception thrown by the try block. It is declared after the try block.

3. finally: Using the finally block we can execute an important piece of code because the finally block will be executed regardless of what the outcome is from the try block.

4. throw: Using the throw keyword we can throw a predefined exception.

5. throws: Using the throws keyword we can declare a new exception from the exception classes.

Java Exception Methods:

SL. No.Method Description 
1public String getMessage()This method returns a string message explaining the exception that occurred.
2public Throwable getCause()This method throws the cause of the occurred exception.
3public String toString()This method returns the message of the getMessage() method along with the class of exception concatenated to it.
4public void printStackTrace()This method returns the output of the toString() method along with its stack trace
5public StackTraceElement [] getStackTrace()This method returns an array containing all the elements of the stack trace.

Example of a program using exception handling using try-catch block:

package com.DataFlair.exceptions;
public class exceptionhandling
{
  public static void main(String[] args) {
    try {
      char[] ch = {'D', 'A', 'T', 'A', 'F', 'L', 'A', 'I', 'R'};
      System.out.println(ch[10]);
    } catch (Exception e) {
      System.out.println("Something went wrong in the indexing of Array");
    } finally {
      System.out.println("The 'try catch' is over, continuing the program from here!");
    }
  }
}

The output of the above Program:

Something went wrong in the indexing of Array
The ‘try catch’ is over, continuing the program from here!

The try-with-resources block:

The try-with-resources block is the most recent addition to the java exception handling. The Java 7 update introduced the try-with-resource block. It automatically closes the resources used within the try-catch block. It is very simple to use, we just have to declare the resources within parenthesis and the resources are automatically closed.

Syntax:

try(FileReader f = new FileReader("Path of the resource file")) {
   // use the resource
   } catch () {
      // body of catch 
   }
}

User-defined or customized Exception in Java:

Not all exceptions are defined in the JVM, there are situations where the programmer might have to define an exception of their own.
Java has the provision to create a customized exception to serve this purpose.

Rules for creating a Java Exception:

To create a customized java exception, we have to keep in mind the following points:

  • The user-defined exception class should extend the in build Exception class.
  • The user must override the toString() method in the user-defined exception class.

Code to create and use a user-defined Exception class:

package com.DataFlair.exceptions;
public class UserDefException extends Exception
{
  private static int year;
  public UserDefException(int year)
  {
    this.year=year;
  }
  public String toString()
  {
    return "Age below 18, Underaged!!!";
  }
  public static void main(String args[]) throws Exception
  {
    UserDefException obj = new UserDefException( 2005 );
    if(year > 2003)
    {
      throw new UserDefException(year);
    }
    else
    {
      System.out.println("Entered year is: " +year);
    }
  }
}

The output of the above code:

Age below 18, Underaged!!!
at com.DataFlair.exceptions.UserDefException.main(UserDefException.java:18)
at com.DataFlair.exceptions.__SHELL0.run(__SHELL0.java:6)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at bluej.runtime.ExecServer$3.lambda$run$0(ExecServer.java:849)
at bluej.runtime.ExecServer.runOnTargetThread(ExecServer.java:964)
at bluej.runtime.ExecServer$3.run(ExecServer.java:846)

So, we can see that the program gives an exception that was created by us and not an inbuilt exception. This can be very useful to check user input for certain criteria.

Important Points to Remember:

  • In a function, more than one statement can throw an exception. A separate try-catch block for each statement is necessary.
  • Each exception is handled by an exception handler. To associate an exception handler to an exception, we need a catch block.
  • A try block can have 0 to n number of catch blocks, but it cannot have more than one finally block.
  • A finally block is optional, it runs regardless of the output of the try block.
  • A try block must have either a catch or finally block.

Conclusion

In this article, we saw the different exceptions present in java and where they are present. We also saw how to handle these exceptions properly to make the program execute smoothly.

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

follow dataflair on YouTube

No Responses

  1. shagufta says:

    Can we use “throws FileNotFoundException” and try-catch() in the same program?

Leave a Reply

Your email address will not be published. Required fields are marked *