Exception Handling

Cards (19)

  • Exception
    An object that describes an unusual or erroneous situation
  • Exception handling
    1. Ignore it (possible only with "unchecked" exception – abnormal termination)
    2. Handle it where it occurs
    3. Handle it in another place in the program
  • Java has a predefined set of exceptions and errors that can occur during execution
  • Error
    Represents an unrecoverable situation and should not be caught
  • try statement

    • Line that throws the exception is executed within a try block
    • A try block is followed by one or more catch clauses
    • Each catch clause has an associated exception type and is called an exception handler
    • When an exception occurs, processing continues at the first catch clause that matches the exception type
  • finally clause
    • Statements in the finally clause always are executed
    • If no exception is generated, the statements in the finally clause are executed after the statements in the try block complete
    • If an exception is generated, the statements in the finally clause are executed after the statements in the appropriate catch clause complete
  • Exception class hierarchy
    • All error and exception classes are descendants of the Throwable class
    • A programmer can define an exception by extending the Exception class or one of its descendants
    • The parent class used depends on how the new exception will be used
  • Checked exceptions
    • Must be explicitly handled by the programmer
    • Compile-time exceptions that extend the Exception class (except RuntimeException and its subclasses)
    • Forced handling: The compiler ensures that you either handle them using try-catch blocks or declare them in the method signature using the throws keyword
  • Unchecked exceptions

    • Do not need explicit handling
    • Run-time exceptions that extend the RuntimeException class
    • Examples: ArithmeticException, NullPointerException
  • Errors
    • Indicate a serious problem that usually occurs due to system resources or other external factors
    • Cannot be caught or handled by the program
    • Occur at run time and are always unchecked
    • Examples: OutOfMemoryError, StackOverflowError, LinkageError
  • A checked exception must either be caught by a method, or be listed in the throws clause of any method that may throw or propagate it
  • A throws clause is appended to the method header
  • The compiler will issue an error if a checked exception is not caught or asserted in a throws clause
  • Unchecked exceptions do not require explicit handling
  • Errors are similar to RuntimeException and its descendants in that errors should not be caught and errors do not require a throws clause
  • throw keyword

    • Used to throw an exception explicitly
    • Specify the exception object which is to be thrown
    • Can throw either checked or unchecked exceptions
  • throws keyword

    • Used to declare an exception
    • Gives information to the programmer that there may occur an exception
    • Used to handle checked exceptions
  • Unchecked exceptions and errors do not need to be declared using throws keyword
  • User-defined exception
    • If it extends Exception class, it is a checked exception
    • If it extends RuntimeException class, it is an unchecked exception