1
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.

Community
  • 1
  • 1
Aman
  • 979
  • 3
  • 10
  • 23

6 Answers6

2

RuntimeException is a subclass of Exception, so your catch block picks up both checked and unchecked exceptions.

Idcmp
  • 659
  • 1
  • 8
  • 19
  • ya i know that but what about objects of Exception class itself. It shows the property of which type? – Aman Apr 22 '15 at 03:53
  • Exception is the root class (Actually it's Throwable). These are checked exceptions, *but* if it's RuntimeException or something extending that makes it unchecked. – seand Apr 22 '15 at 03:56
  • If you were to throw an instance of Exception, you would need to say "void method() throws Exception". Callers of method() must handle Exception directly. (But please don't throw Exception :-) – Idcmp Apr 22 '15 at 03:57
  • 1
    @Aman - An `Exception` is only checked or unchecked when you have a concrete instance to work with. When you don't, as in the case of simply writing `catch (Exception e) {...}`, the `Exception` is of _both_ types, or neither, depending upon your preference. This is because `Exception` is the shared superclass of all checked and unchecked exceptions. When used in that way, it includes both types. When a concrete instance is constructed, it's only one. – aroth Apr 22 '15 at 04:06
2

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.

Community
  • 1
  • 1
Makoto
  • 104,088
  • 27
  • 192
  • 230
1

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.

Tony Vu
  • 4,251
  • 3
  • 31
  • 38
  • Thanks for the link ... it gave My answer. But then please tell why the code compiles fine because no checked exception is thrown from try block. – Aman Apr 22 '15 at 03:56
  • 1
    I'm not sure this really answers the question. There's a matter of context that needs to be considered. When you construct a `new Exception()` and throw it, that's a checked exception. You'll be required to declare it against your method, and callers will be required to catch it. However, when you write `catch (Exception e) {}`, you're saying that you want to catch _both_ checked and unchecked exceptions. The important point is, an `Exception` (or subclass thereof) can only be said to be checked or unchecked when a concrete instance has been constructed. – aroth Apr 22 '15 at 04:01
  • The difference between checked and unchecked exception is that - Checked Exceptions: You have to catch it because it expects you to do something to recover from the exception. If you write come code which may throw checked exception without a catch block, it won't compile. In your case, you have something which will not throw checked exception and still have catch block, so it still compiles even though you do not need to include catch block. - Unchecked Exception: You do not have to write the catch block and it normally represents unexpected or unrecoverable situation. – Tony Vu Apr 22 '15 at 04:13
1
  • 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.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

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.

Sunil Rajashekar
  • 350
  • 2
  • 18
0

Exception class itself is not a checked Exception..Checked Exceptions are exceptions that are child of Exception class e-g 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 .e-g

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 e-g

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