简体   繁体   中英

Exception class Object is of type Checked or Unchecked

import java.io.*;
class a {
    public static void main(String [] args) {
        try {
            new a().go();
        }
        catch(Exception e) {
            System.out.println("catch");
        }   
    }

    void go() {}
}

Already visited this link but didn't got my answer

  • If Exception class object is considered Checked : This code compiles fine even though Exception object will never be thrown from go method. EXACTLY the checked exceptions that can be thrown from the try block are handled and no other. So it cannot be checked.

  • If Exception class object is considered UnChecked : Exception class is not subclass of Error or RuntimeException so it cannot be unchecked.

Please help me to understand ... it is an object of which type.

RuntimeException是Exception的子类,因此您的catch块将同时获取已检查和未检查的异常。

Check this link http://docs.oracle.com/javase/7/docs/api/java/lang/Exception.html

The class Exception and any subclasses that are not also subclasses of RuntimeException are checked exceptions

So to answer your question, it is a checked exception.

Exception is a checked exception. It's more of an exclusion really; it's neither an Error nor a RuntimeException , which are both considered unchecked.

The unchecked exception classes are the run-time exception classes and the error classes.

The checked exception classes are all exception classes other than the unchecked exception classes. That is, the checked exception classes are all subclasses of Throwable other than RuntimeException and its subclasses and Error and its subclasses.

That said, your contrived example will still compile because Exception is the superclass to all runtime exceptions, which (for some reason) are expected to be caught in your code block. Since you don't declare Exception to be thrown on go , you are not required to catch it as it is not a checked exception.

Do not use code like this in production, as any checked exception or runtime exception will be caught by that block.

  • If Exception class object is considered Checked

It is.

This code compiles fine even though Exception object will never be thrown from go method.

You can't know that. There could be a RuntimeException.

EXACTLY the checked exceptions that can be thrown from the try block are handled and no other.

That's not correct. The checked exceptions and the runtime exceptions are caught.

So it cannot be checked.

Invalid deduction from false premisses.

  • If Exception class object is considered UnChecked

It is checked. No need to consider the rest of this.

Exception class is not subclass of Error or RuntimeException so it cannot be unchecked.

Correct.

Exception class is considered Checked exception.

Checked exception means you need to enclose the method that throws Exception in Try catch
else you need to add throws declaration so that the parent method that called it would handle exeception.

Exception class itself is not a checked Exception..Checked Exceptions are exceptions that are child of Exception class eg IOException etc There are two things..
1- are you throwing it ??
2- are you talking about declaring it in catch clause.??

LETS DEMONSTRATE IT WITH EXAMPLE

1- if you are throwing any Exception class Except (RuntimeException or its child classes) then you have to handle it by catch clause or declare it by throws..no matter if it is Exception class or Throwable or other Checked Exceptions .eg

void go() throws Exception
{ 
throw new Exception();
} //you have to handle or declare it

now calling method should have to handle or declare it

2- YOUR POINT IS HERE are you talking about declaring it in catch clause. (EXCEPTION VS OTHER CHECKED EXCEPTION)
Exception don't need to be thrown if declared in catch but checked Exception need to b thrown if catched..(simple answer is that)

void callingMethod()
{
  try{ } // NO NEED TO THROW Exception OR Thowable
  catch(Exception e){ 
      e.printStakeTrace(); 
  }
}

//IN CASE OF CHECKED EXCEPTIONS THAT ARE CHILD OF EXCEPTION YOU HAVE TO THROUGH IT IF YOU ARE CATCHING IT>>OTHERWISE COMPILER WILL TELL YOU TO THROUGH IT eg

void callingMethod()
{
  try{ } // here is compiler error..it says you have to through it
  catch(IOException e){ 
      e.printStakeTrace(); 
  }
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM