Java exception list

Abel 101 Published: 12/15/2024

Java exception list

I'll respond in English as per your request!

Here's a comprehensive list of common Java exceptions:

ArrayIndexOutOfBoundsException: This exception occurs when you try to access an array or object with an index that is out of bounds.

Example: int[] array = new int[5]; System.out.println(array[6]);

ArithmeticException: This exception is thrown if you attempt to perform an arithmetic operation on a number that cannot be expressed as a finite decimal fraction, such as dividing by zero or attempting to calculate the square root of a negative number.

Example: int x = 5/0;

ClassCastException: This exception occurs when you try to cast an object to a subclass of which it is not an instance.

Example: Object obj = "hello"; String str = (String) obj;

IndexOutOfBoundsException: This exception is thrown if the index used to access an array, vector, or other type of indexed object is out of bounds.

Example: String[] array = new String[5]; System.out.println(array[6]);

InstantiationException: This exception occurs when you try to create a class using the Class.forName() method and the specified name is not a valid class name.

Example: try { Class<?> clazz = Class.forName("InvalidClass"); } catch (ClassNotFoundException e) { }

NullPointerException: This exception occurs when you try to access an object reference that has been set to null.

Example: String str = null; System.out.println(str.length());

NumberFormatException: This exception is thrown if you attempt to convert a string to a number (integer, float, etc.) but the string does not represent a valid number.

Example: int x = Integer.parseInt("hello");

RuntimeException: This exception occurs when a method has been invoked that should not be called for any reason.

Example: public class MyClass { public void doSomething() throws RuntimeException { } }

StackOverflowError: This exception is thrown if you create a very large recursion and the stack overflows.

Example: void foo() { foo(); } foo();

UnsupportedOperationException: This exception occurs when you attempt to perform an operation that is not supported by a particular class.

Example: List<String> list = new LinkedList<>(); list.add(5);

NumberFormatException (part of the Java API): This exception occurs if you attempt to convert a string to a number but the string does not represent a valid number.

Example: int x = Integer.parseInt("hello");

ParserConfigurationException: This exception is thrown when an application tries to create a new SAXParser or DocumentBuilderFactory, but the system cannot create it.

Example: try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); } catch (ParserConfigurationException e) { }

XMLErrorException: This exception occurs if there is a problem parsing XML.

Example: try { SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); } catch (XMLParseException e) { }

Note that this list is not exhaustive, and you can get more information about the exceptions in Java by referring to official documentation or online resources.

What are exception classes in java with examples

Exception handling is a crucial part of programming in Java. Exception classes in Java are used to handle and manage runtime errors that occur during the execution of a program.

There are several types of exceptions in Java, including:

RuntimeException: This class is the superclass of most exception classes and provides methods for generating and throwing exceptions.

Example:

public class Main {

public static void main(String[] args) {

try {

int i = Integer.parseInt("abc");

} catch (NumberFormatException e) {

System.out.println("Error: " + e.getMessage());

}

}

}

In this example, the Integer.parseInt() method throws a NumberFormatException when trying to parse an invalid integer. The catch block catches and prints the error message.

Exception: This is the superclass of all exceptions in Java. It provides methods for generating and throwing exceptions.

Example:

public class Main {

public static void main(String[] args) {

try {

int i = Integer.parseInt("abc");

} catch (Exception e) {

System.out.println("Error: " + e.getMessage());

}

}

}

In this example, the same as the previous one, but using Exception instead of RuntimeException.

IOException: This class represents input/output-related exceptions.

Example:

public class Main {

public static void main(String[] args) {

try (BufferedReader reader = new BufferedReader(new FileReader("file.txt"))) {

String line = reader.readLine();

} catch (IOException e) {

System.out.println("Error: " + e.getMessage());

}

}

}

In this example, the code attempts to read a file. If there is an error reading the file (e.g., it doesn't exist), an IOException is thrown and caught in the catch block.

ArithmeticException: This class represents arithmetic-related exceptions, such as division by zero or overflow.

Example:

public class Main {

public static void main(String[] args) {

try {

int i = 10 / 0;

} catch (ArithmeticException e) {

System.out.println("Error: " + e.getMessage());

}

}

}

In this example, the code attempts to divide by zero. An ArithmeticException is thrown and caught in the catch block.

ArrayIndexOutOfBoundsException: This class represents array index-related exceptions.

Example:

public class Main {

public static void main(String[] args) {

try {

int[] arr = {1, 2, 3};

System.out.println(arr[4]);

} catch (ArrayIndexOutOfBoundsException e) {

System.out.println("Error: " + e.getMessage());

}

}

}

In this example, the code attempts to access an element in an array that doesn't exist. An ArrayIndexOutOfBoundsException is thrown and caught in the catch block.

ClassCastException: This class represents exceptions that occur when trying to cast an object to a type that it's not compatible with.

Example:

public class Main {

public static void main(String[] args) {

try {

Object obj = new String("Hello");

String str = (String) obj;

} catch (ClassCastException e) {

System.out.println("Error: " + e.getMessage());

}

}

}

In this example, the code attempts to cast an object that is not a String type. A ClassCastException is thrown and caught in the catch block.

These are just some examples of exception classes in Java and how they can be used to handle runtime errors.