How to fix "Exception in thread main" in java?


An exception is an issue (run time error) occurred during the execution of a program. When an exception occurred the program gets terminated abruptly and, the code past the line that generated the exception never gets executed.

Example

import java.util.Scanner;
public class ExceptionExample {
   public static void main(String args[]) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter first number: ");
      int a = sc.nextInt();
      System.out.println("Enter second number: ");
      int b = sc.nextInt();
      int c = a/b;
      System.out.println("The result is: "+c);
   }
}

Output

Enter first number:
100
Enter second number:
0
Exception in thread "main" java.lang.ArithmeticException: / by zero
at ExceptionExample

Types of exceptions

In Java There are two types of exceptions

  • Checked Exception − A checked exception is an exception that occurs at the time of compilation, these are also called as compile time exceptions. These exceptions cannot simply be ignored at the time of compilation; the programmer should take care of (handle) these exceptions.
  • Unchecked Exception − An unchecked exception is the one which occurs at the time of execution. These are also called as Runtime Exceptions. These include programming bugs, such as logic errors or improper use of an API. Runtime exceptions are ignored at the time of compilation.

Exception in thread main

The display pattern of the runtime exception/unchecked exception is "Exception in thread main" i.e. whenever a runtime exception occurs the message starts with this line.

Example

In following Java program, we have an array with size 5 and we are trying to access the 6th element, this generates ArrayIndexOutOfBoundsException.

public class ExceptionExample {
   public static void main(String[] args) {
      //Creating an integer array with size 5
      int inpuArray[] = new int[5];
      //Populating the array
      inpuArray[0] = 41;
      inpuArray[1] = 98;
      inpuArray[2] = 43;
      inpuArray[3] = 26;
      inpuArray[4] = 79;
      //Accessing index greater than the size of the array
      System.out.println( inpuArray[6]);
   }
}

Run time exception

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
at MyPackage.ExceptionExample.main(ExceptionExample.java:14)

Example

In the following example we are trying to create an array by using a negative number for the size value, this generates a NegativeArraySizeException.

public class Test {
   public static void main(String[] args) {
      int[] intArray = new int[-5];
   }
}

Run time exception

On executing, this program generates a run time exception as shown below.

Exception in thread "main" java.lang.NegativeArraySizeException
at myPackage.Test.main(Test.java:6)

Handling runtime exceptions

You can handle runtime exceptions and avoid abnormal termination but, there is no specific fix for runtime exceptions in Java, depending on the exception, type you need to change the code.

For example, if you need to fix the ArrayIndexOutOfBoundsException in the first program listed above you need to remove/change the line that accesses index positon of the array beyond its size.

Example

public class ExceptionExample {
   public static void main(String[] args) {
      //Creating an integer array with size 5
      int inpuArray[] = new int[5];
      //Populating the array
      inpuArray[0] = 41;
      inpuArray[1] = 98;
      inpuArray[2] = 43;
      inpuArray[3] = 26;
      inpuArray[4] = 79;
      //Accessing index greater than the size of the array
      System.out.println( inpuArray[3]);
   }
}

Output

26

Updated on: 02-Jul-2020

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements