English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Exceptions are problems that occur during program execution (runtime errors). When an exception occurs, the program will suddenly terminate, and the code after the line where the exception occurred will never be executed.
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 Result
Enter first number: 100 Enter second number: 0 Exception in thread "main" java.lang.ArithmeticException: / by zero at ExceptionExample
In Java, there are two types of exceptions
Checked exception - Checked exceptions are exceptions that occur during compilation. These are also known as compile-time exceptions. These exceptions cannot be simply ignored at compile time. Programmers should pay attention to (handle) these exceptions.
Unchecked exception - Unchecked exceptions are exceptions that occur during execution. These are also known as runtime exceptions. They include programming errors, such as logical errors or improper API usage. Runtime exceptions are ignored during compilation.
Runtime Exception/The display mode of unchecked exceptions is“Thread main exception”,that is, the message is always prefixed with that line when a runtime exception occurs.
In the following Java program, we have an array of size5and attempts to access the6elements, which will generate ArrayIndexOutOfBoundsException.
public class ExceptionExample { public static void main(String[] args) { //of size5of an integer array int inputArray[] = new int[5]; //Filling the array inputArray[0] = 41; inputArray[1] = 98; inputArray[2] = 43; inputArray[3] = 26; inputArray[4] = 79; //Accessing index greater than the size of the array System.out.println(inputArray[6); } }
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6 at MyPackage.ExceptionExample.main(ExceptionExample.java:14)
In the following example, we try to create an array using a negative number as the size value, which will generate NegativeArraySizeException.
public class Test { public static void main(String[] args) { int[] intArray = new int[-5]; } }
When the program is executed, it will generate a runtime exception as follows.
Exception in thread "main" java.lang.NegativeArraySizeException at myPackage.Test.main(Test.java:6)
You can handle runtime exceptions and avoid abnormal termination, but there is no specific fix for runtime exceptions in Java, and the type of code change required depends on the exception.
For example, if you need to fix ArrayIndexOutOfBoundsException in the first program listed above, you need to delete/Change the line where the array index access exceeds its size.
public class ExceptionExample { public static void main(String[] args) { //Creating an integer array with size 5 int inputArray[] = new int[5]; //Filling the array inputArray[0] = 41; inputArray[1] = 98; inputArray[2] = 43; inputArray[3] = 26; inputArray[4] = 79; //Accessing index greater than the size of the array System.out.println(inputArray[3); } }
Output Result
26