4 Replies Latest reply: Jun 16, 2012 9:03 AM by rp0428 RSS

    What makes subclass of exception checked exception

    824461
      Hi

      When I make a exception class

      class TestException extends Exception{}

      This exception is a "checked exception". Understood
      But the predefined java class RunTimeException is also a subclass of Exception what makes it an unchecked exception. Does compiler treat this class differently.

      How I can make unchecked exception class when I extend my custom exception class from Exception.

      Thanks
      Abhishek
        • 1. Re: What makes subclass of exception checked exception
          EJP
          But the predefined java class RunTimeException is also a subclass of Exception what makes it an unchecked exception.
          The fact that it extends RuntimeException.
          Does compiler treat this class differently.
          Yes.
          How I can make unchecked exception class when I extend my custom exception class from Exception.
          Make it extend RuntimeException. If you're sure that's what you want to do. In my opinion there are already enough exceptions that do that, if you need one, and application exceptions should be checked.
          • 2. Re: What makes subclass of exception checked exception
            824461
            But the RunTimeException class is

            Class RunTimeException extends Exception
            {
            }

            As per this RunTimeException should also be checked exception, When you say compiler treats it differently, isn't it violating the general rule "if you extend custom exception class with Exception then it will be a checked exception".

            I can extend it from RunTimeException but I just wanted to know the valid reason behind it. I expect compiler to treat all the class equally.
            • 3. Re: What makes subclass of exception checked exception
              EJP
              isn't it violating the general rule "if you extend custom exception class with Exception then it will be a checked exception"?
              No, because there is no such rule. You made it up.

              The rule is that an exception class that extends Exception is a checked exception unless it extends RuntimeException.

              Is this really so mysterious?
              • 4. Re: What makes subclass of exception checked exception
                rp0428
                >
                But the predefined java class RunTimeException is also a subclass of Exception what makes it an unchecked exception. Does compiler treat this class differently.

                How I can make unchecked exception class when I extend my custom exception class from Exception.
                >
                The answer to both of these questions can be found in Chapter 11.2 'Compile-Time Checking of Exceptions of the Java Language Specification. Suggest you read the entire section.
                http://docs.oracle.com/javase/specs/jls/se5.0/html/exceptions.html
                >
                The unchecked exceptions classes are the class RuntimeException and its subclasses, and the class Error and its subclasses. All other exception classes are checked exception classes.
                >
                These two other subsections give the reasons the designers of Java made the choices above
                >
                11.2.4 Why Errors are Not Checked

                11.2.5 Why Runtime Exceptions are Not Checked