Posts Tagged: "exceptions"

Java – ArithmeticException

ArithmeticException: Thrown when an exceptional arithmetic condition occurs, such as division by zero.Example: int a = 10;int b = 0;try { System.out.println(a / b);} catch (ArithmeticException e) { System.out.println("Caught ArithmeticException: " + e.getMessage());}

Java – IllegalArgumentException

IllegalArgumentException: Thrown to indicate that a method has been passed an illegal or inappropriate argument.Example: public static void checkAge(int age) { if (age < 18) { throw new IllegalArgumentException("Age must be at least 18"); }}try { checkAge(16);} catch (IllegalArgumentException e) { System.out.println("Caught IllegalArgumentException: " + e.getMessage());}

Java – NumberFormatException

NumberFormatException: Thrown when an attempt to convert a string to a numeric type fails.Example: String str = "ABC";try { int number = Integer.parseInt(str);} catch (NumberFormatException e) { System.out.println("Caught NumberFormatException: " + e.getMessage());}

Java – FileNotFoundException

FileNotFoundException: Thrown when an attempt to open the file denoted by a specified pathname has failed.Example: import java.io.File;import java.io.FileNotFoundException;import java.util.Scanner;try { File file = new File("nonexistent.txt"); Scanner sc = new Scanner(file);} catch (FileNotFoundException e) { System.out.println("Caught FileNotFoundException: " + e.getMessage());}

Java – InterruptedException

InterruptedException: Thrown when a thread is waiting, sleeping, or otherwise occupied, and the thread is interrupted.Example: try { Thread.sleep(1000);} catch (InterruptedException e) { System.out.println("Caught InterruptedException: " + e.getMessage());}

Java – IOException

IOException: A general exception thrown when an I/O operation fails or is interrupted.Example: import java.io.FileReader;import java.io.IOException;try { FileReader reader = new FileReader("test.txt");} catch (IOException e) { System.out.println("Caught IOException: " + e.getMessage());}

Java – NoSuchElementException

NoSuchElementException: Thrown when one tries to access an element that is not present, such as an empty iterator.Example: import java.util.Iterator;import java.util.NoSuchElementException;Iterator iterator = new ArrayList().iterator();try { System.out.println(iterator.next());} catch (NoSuchElementException e) { System.out.println("Caught NoSuchElementException: " + e.getMessage());}

Java – ConcurrentModificationException

ConcurrentModificationException: Thrown when an object is concurrently modified while iterating through it, in a way that is not allowed.Example: import java.util.ArrayList;import java.util.Iterator;import java.util.ConcurrentModificationException;ArrayList list = new ArrayList();list.add("One");list.add("Two");list.add("Three");Iterator iterator = list.iterator();try { list.remove("Two"); iterator.next();} catch (ConcurrentModificationException e) { System.out.println("Caught ConcurrentModificationException: " + e.getMessage());}

Java – IllegalStateException

IllegalStateException: Thrown when a method has been invoked at an illegal or inappropriate time.Example: import java.util.Iterator;import java.util.NoSuchElementException;Iterator iterator = new ArrayList().iterator();try { iterator.remove();} catch (IllegalStateException e) { System.out.println("Caught IllegalStateException: " + e.getMessage());}

Java – UnsupportedOperationException

UnsupportedOperationException: Thrown to indicate that a requested operation is not supported.Example: import java.util.Collections;import java.util.List;List list = Collections.unmodifiableList(new ArrayList());try { list.add("Hello");} catch (UnsupportedOperationException e) { System.out.println("Caught UnsupportedOperationException: " + e.getMessage());}