Exceptions FAQ's
Exception is like an error. When an exception occurs, the program execution terminates at that line itself.

It is a mechanism to handle runtime errors in a smooth and controlled way.
Helps to maintain program flow without terminating it.

Exception Handling methods are:

  • try
  • catch
  • finally
  • throw
  • throws
  • try block → Code that may throw exception
  • catch block → Code to handle the exception
  • finally block → Always executes, used for cleanup code (like closing files)

Yes, multiple catch blocks are allowed for different exception types in the form of parent class and sub class combinations.

Yes, using multi-catch with | (pipe) symbol.

  • throw → Used to explicitly throw an exception inside the method - only one exception can be thrown at a time.
  • throws → Used in method signature to declare possible exceptions, we can declare more than one exception at a time.
Throwable is the parent class for all exceptions and errors in Java.
It has two direct subclasses:
 - Exception
 - Error
Exceptions are divided into:
 - Checked Exceptions (Compile-time Exceptions)
 - Unchecked Exceptions (Runtime Exceptions)

  • Checked Exceptions (Compile-time)
    Must be handled using try-catch or throws.
    Example: IOException, SQLException
  • Unchecked Exceptions (Runtime)
    Not mandatory to handle.
    Example: NullPointerException, ArithmeticException

Checked Exceptions:

  • IOException
  • FileNotFoundException
  • SQLException

Unchecked Exceptions:

  • ArithmeticException
  • NullPointerException
  • IndexOutOfBoundsException
  • StringIndexOutOfBoundsException
  • InputMismatchException
  • ArrayIndexOutOfBoundsException
  • NumberFormatException

Yes, you can, but it’s not recommended.
Throwable includes Error, which usually shouldn’t be caught.

  • Exception → Recoverable (e.g., file not found, invalid input)
  • Error → Not recoverable (e.g., OutOfMemoryError, StackOverflowError)

No, it will cause a compilation error.
The child class catch block must come first, then the parent.