Common Java Exception

Common Java Exceptions

The exception method in the Java programming language is used to control and manage common or complex issues and program conditions that occur during the run/execution process of an existing Java program. Java programming language defines a set of pre-existing exception lists that help Java users manage and control common errors that occur during program runtime. These Java exception lists are a built-in feature of the Java API and are primarily divided into two categories: checked exceptions and unchecked exceptions.

Common Java Exception

Common Java exception list categories.

Here is a list of some of the most common Java exceptions that occur during the creation and execution of a Java program.

NullPointerException/Unchecked Exception Java exception.

A NullPointerException is generated in a Java program when the JVM installed in Java attempts to use a null reference as a class object. For example, calling a method on a null object or accessing a user-defined null array of data.

Example of a Java NullPointerException.

public class NullPointerIllustration {

public static void main(String[] args) {

String strvalue = null;

// here This will throw a NullPointerException in a Java program

System.out.println(strvalue.length());

}

}

Result – Exception in thread “main” java.lang.NullPointerException

ArrayIndexOutOfBoundsException/Unchecked Exception Java Exception.

An ArrayIndexOutOfBoundsException is generated in a Java program when an attempt is made to access a user-defined array with an invalid index location (an array index less than 0 or greater than or equal to the array’s length).

An example of an ArrayIndexOutOfBoundsException.

public class ArrayIndexOutOfBoundsIllustration {

public static void main(String[] args) {

int[] array = {9, 12, 14, 16, 22};

// here This will throw an ArrayIndexOutOfBoundsException in this program

System.out.println(array[7]);

}

}

Result – Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: Index 7 is out of bounds for length 5.

ArithmeticException/Unchecked Exception Java Exception.

An ArithmeticException error is generated in a Java program when a Java programmer attempts to perform an invalid or illegal arithmetic operation, such as dividing a numeric value by zero.

Java ArithmeticException example.

public class ArithmeticExceptionIllustration {

public static void main(String[] args) {

int p = 30;

int q = 0;

/ here This will throw an ArithmeticException error when dividing by a zero value.

System.out.println(p / q);

}

}

Result – Exception in thread “main” java.lang.ArithmeticException: / by zero

ClassNotFoundException/Checked Exception Java exception.

A ClassNotFoundException is generated in a Java program when an application in a Java user program attempts to load a class, but the required class is not found. For example, by using reflection or dynamically loading the class.

Example of ClassNotFoundException.

public class ClassNotFoundIllustration {

public static void main(String[] args) {

try {

/ Here we use a try block to load a non-existent class in the program

Class.forName(“com.nonexistent.Class”);

} catch (ClassNotFoundException e) { // Use a catch block

System.out.println(“Your searched class is not found!”);

}

}

}

Result – Your searched class is not found!

FileNotFoundException/CheckedException Java exception.

A FileNotFoundException is generated in a Java program when a Java user tries to open a file that does not exist, or that cannot be accessed by the current program.

Java FileNotFoundException example.

import java.io.*;

public class FileNotFoundExceptionIIllustration {

public static void main(String[] args) {

try {

FileReader file = new FileReader(“notexistFile.txt”);

} catch (FileNotFoundException e) {

System.out.println(“Your searched file is not found!”);

}

}

}

Result – Your searched file is not found!

IOException/Checked Exception Java exception.

In Java programs, IOException is a general program exception type for common program issues related to input-output, such as failure to read or write a data file, or failure to read from a socket.

Example of an IOException Java exception.

import java.io.*;

public class IOExceptionIllustration {

public static void main(String[] args) {

try {

FileReader file = new FileReader(“sampleFile.txt”);

file.read();

} catch (IOException e) {

System.out.println(“IOException generated during file reading here”);

}

}

}

Result – IOException occurred during file reading here

IndexOutOfBoundsException/Unchecked Exception Java exception.

An IndexOutOfException is generated in a Java program when the Java user attempts to access an invalid index location in a list or other collection data type, such as an ArrayList.

IndexOutOfBoundsException example of a Java exception.

import java.util.*;

public class IndexOutOfBoundsExceptionIllustration {

public static void main(String[] args) {

List<String> list = new ArrayList<>();

list.add(“Macbook Pro”);

list.add(“Asus Tuf”);

list.add(“Hp Pavilion”);

// here This will throw an IndexOutOfBoundsException during program exception

System.out.println(list.get(7));

}

}

Result – Exception in thread “main” java.lang.IndexOutOfBoundsException: Index 7 out of bounds for length 3

IllegalArgumentException/Unchecked Exception Java exception.

IllegalArgumentException is generated in a Java program when a user-defined class method detects an argument that is false or illegal according to the rules.

Instance of IllegalArgumentException.

public class IllegalArgumentExceptionIllustration {

public static void main(String[] args) {

int value = -21;

if (value < 0) {

throw new IllegalArgumentException(“here you cannot set value as negative”);

}

}

}

Result – Exception in thread “main” java.lang.IllegalArgumentException: Here you cannot set value as negative.

NumberFormatException/Unchecked Exception Java exception.

NumberFormatException is generated in a Java program when a Java user attempts to convert a string data type value to a number data type. For example, by using the Integer.parseInt() function method and the string is not a valid number as a data type.

Instance of NumberFormatException.

public class NumberFormatExceptionIllustration {

public static void main(String[] args) {

String strtext = “Vcanhelpsu”;

/ here This will throw NumberFormatException during str to integer conversion

int integer = Integer.parseInt(strtext);

}

}

Result – Exception in thread “main” java.lang.NumberFormatException: For input string: “Vcanhelpsu”

ConcurrentModificationException/Unchecked Exception Java exception.

ConcurrentModificationException is generated in a Java program when a user-defined collection data type, such as a list, is modified in a structural order or iterated over using a looping concept. For example, modifying a list while using the loop iterator method is thrown.

Example of ConcurrentModificationException.

import java.util.*;

public class ConcurrentModificationExceptionIllustration {

public static void main(String[] args) {

List<String> list = new ArrayList<>();

list.add(“Java”);

list.add(“Python”);

list.add(“Ruby”);

list.add(“Pearl”);

for (String item : list) {

if (item.equals(“Python”)) {

list.remove(item); // This will throw a ConcurrentModificationException (program error)

}

}

}

}

Result – Exception in thread “main” java.util.ConcurrentModificationException

NoSuchElementException/Unchecked Exception Java Exception.

A NoSuchElementException is generated in a Java program when the current program attempts to access element data that is not already in a collection or loop iterator. For example, calling the next() function when there are no elements left in the iterator.

NoSuchElementException example.

import java.util.*;

public class NoSuchElementExceptionIllustration {

public static void main(String[] args) {

List<String> list = new ArrayList<>();

list.add(“P”);

list.add(“Q”);

Iterator<String> iterator = list.iterator();

// here it Moving to the past all arraylist elements

iterator.next();

iterator.next();

// here This will throw NoSuchElementException program exception during run

iterator.next();

}

}

Result – Exception in thread “main” java.util.NoSuchElementException

SecurityException/UncheckedException Java Exception.

A SecurityException is generated in a Java program when a security manager in the Java language restricts user access to a Java program resource, such as a file or network resource, based on security restrictions.

Example of SecurityException.

public class SecurityExceptionIllustration {

public static void main(String[] args) {

System.setSecurityManager(new SecurityManager());

// This will throw a SecurityException error if the security manager restricts any user permission.

System.exit(0);

}

}

Result – Exception in thread “main” java.lang.UnsupportedOperationException: Setting a Security Manager is not supported

Leave a Reply