Mainstream Exceptions in Java: Checked and Unchecked Types

Respondido usando: GPT-4o-mini porJava Q&A Hub

Pergunta:

What are the mainstream Exceptions in Java? Please list them one by one.

Resposta:

In Java, exceptions are categorized into two main types: checked exceptions and unchecked exceptions. Here are some of the mainstream exceptions in Java:

Checked Exceptions

These exceptions are checked at compile-time. The programmer is required to handle these exceptions explicitly.

  1. IOException - Signals that an I/O exception of some sort has occurred.
  2. SQLException - Provides information on a database access error or other errors.
  3. ClassNotFoundException - Thrown when an application tries to load a class through its string name but cannot find the definition of the class.
  4. FileNotFoundException - Signals that an attempt to open the file denoted by a specified pathname has failed.
  5. ParseException - Indicates that an error has occurred while parsing a string.
  6. InterruptedException - Thrown when a thread is interrupted while it is waiting, sleeping, or otherwise occupied.
  7. NoSuchMethodException - Thrown when a particular method cannot be found.
  8. InvocationTargetException - Wraps an exception thrown by an invoked method or constructor.
  9. CloneNotSupportedException - Thrown when the clone() method in class Object is called to clone an object, but the object's class does not implement the Cloneable interface.

Unchecked Exceptions

These exceptions are not checked at compile-time. They are subclasses of RuntimeException.

  1. NullPointerException - Thrown when the application attempts to use null in a case where an object is required.
  2. ArrayIndexOutOfBoundsException - Thrown to indicate that an array has been accessed with an illegal index.
  3. ArithmeticException - Thrown when an exceptional arithmetic condition has occurred, such as division by zero.
  4. ClassCastException - Thrown when an object is cast to a class of which it is not an instance.
  5. IllegalArgumentException - Thrown to indicate that a method has been passed an illegal or inappropriate argument.
  6. IllegalStateException - Signals that a method has been invoked at an illegal or inappropriate time.
  7. IndexOutOfBoundsException - Thrown to indicate that an index of some sort (e.g., an array or string) is out of range.
  8. UnsupportedOperationException - Thrown to indicate that the requested operation is not supported.
  9. ConcurrentModificationException - Thrown when a collection is modified while iterating over it in a way that is not allowed.

These exceptions are commonly encountered in Java programming and are essential for error handling and debugging.