What are Checked Exceptions ?

Discussion in 'Exceptions and Assertions' started by vbraojava, Sep 23, 2008.

  1. vbraojava

    vbraojava New Member

    Exceptions which are detected by Java Compiler at compile time is called Checked Exceptions.These are exceptional conditions that a well-written application should anticipate and recover from.

    Example : suppose an application prompts a user for an input file name, then opens the file by passing the name to the constructor for java.io.FileReader.

    - Normally, the user provides the name of an existing, readable file, so the construction of the FileReader object succeeds, and the execution of the application proceeds normally.
    But sometimes the user supplies the name of a nonexistent file, and the constructor throws java.io.FileNotFoundException. A well-written program will catch this Exception and notify the user of the mistake, possibly prompting for a corrected file name.

    - All Exceptions are Checked Exceptions, except for those indicated by Error, RuntimeException, and their subclasses.
  2. Checked Exception

    • represent invalid conditions in areas outside the immediate control of the program (invalid user input, database problems, network outages, absent files)

    • are subclasses of Exception

    • a method is obliged to establish a policy for all Checked Exceptions thrown by its implementation (either pass the checked exception further up the stack, or handle it somehow)

    A Compiler for the Java programming language checks, at compile time, that a program contains handlers for Checked Exceptions, by analyzing which Checked Exceptions can result from execution of a method or constructor. For each Checked Exception which is a possible result, the throws clause for the method or constructor must mention the class of that exception or one of the superclasses of the class of that exception. This compile-time checking for the presence of exception handlers is designed to reduce the number of exceptions which are not properly handled.
  3. anneroberts

    anneroberts Member

    A checked special case is a sort of exemption that must be either gotten or proclaimed in the technique in which it is tossed. For instance, the java.io.IOException is a checked exemption.

Share This Page