Strange, isn't it?

Hi guys,

just a very simple little question about Exceptions handling...

I think that the class 'Exception' is a checked exception. So when I write a method like that:

void myMethod()throws Exception{}

I have to enclose it in a try/catch block. Or I get something like this at compile time:

unreported exception java.lang.Exception; must be caught or declared to be thrown

On the other hand, when I write a try/catch block, all the checked exceptions specified in the catch blocks have to be declared by the methods called from the try.

For example, the following gives me an error:

try{

System.out.println("Hello!");// Doesn't throw checked exception

}

catch (IOException x){}// Checked Exception

And the error is:

exception java.io.IOException is never thrown in body of correspondingtry statement

What I don't understand is that when I try this code, the compiler doesn't complain:

try{

System.out.println("Hello!");// Doesn't throw checked exception

}

catch (Exception x){}// Checked Exception too!

What does it mean? Isn't the class 'Exception' a checked exception? Or is it sometimes possible to catch checked exceptions that are never thrown?

Thank you very much for your help!!

[1997 byte] By [Cedric_acca] at [2007-11-27 11:36:28]
# 1

> Isn't the class 'Exception' a checked exception?

RuntimeException extends Exception. In other words, not necessarily.

~

yawmarka at 2007-7-29 17:08:57 > top of Java-index,Java Essentials,Java Programming...
# 2

> What does it mean? Isn't the class 'Exception' a checked exception? Or is it > sometimes possible to catch checked exceptions that are never thrown?

There's a technical reason for this, and I'm sure someone will be

quote chapter and verse of the JLS soon enough, but it comes

down to the compiler cutting you some slack. RuntimeException

is a non-checked exception and a subclass of Exception, so

that catch block could legitimately be catching RuntimeExceptions.

BigDaddyLoveHandlesa at 2007-7-29 17:08:57 > top of Java-index,Java Essentials,Java Programming...
# 3

> Hi guys,

>

> just a very simple little question about Exceptions

> handling...

> I think that the class 'Exception' is a checked

> exception. So when I write a method like that:

> void myMethod() throws Exception {}

> I have to enclose it in a try/catch block.

Because it's checked.

That's what "checked" means--you have to catch or declare that you throw it.

> On the other hand, when I write a try/catch block,

> all the checked exceptions specified in the catch

> blocks have to be declared by the methods called from

> the try.

> For example, the following gives me an error:

> try {

> System.out.println("Hello!"); // Doesn't throw

> ow checked exception

> }

> catch (IOException x) {} // Checked

> d Exception

> And the error is:

> exception java.io.IOException is never thrown

> in body of corresponding try statement

The compiler knows that IOE is never thrown there (because IOE is checked, so if it were thrown, it would be declared by println). It's telling you there's no point in trying to catch an exception that can't occur.

>

> What I don't understand is that when I try this code,

> the compiler doesn't complain:

> try {

> System.out.println("Hello!"); // Doesn't throw

> ow checked exception

> }

> catch (Exception x) {} // Checked Exception

> n too!

Because, while Exception is checked, its child RuntimeException is not, can catching an exception will also catch all that exception's subclasses. The compiler can't tell whether RuntimeException or one of its descendants could be thrown, because they're unchecked, so it allows you to catch them.

jverda at 2007-7-29 17:08:57 > top of Java-index,Java Essentials,Java Programming...
# 4

> > Isn't the class 'Exception' a checked

> exception?

>

> RuntimeException extends Exception. In other words,

> not necessarily.

>

> ~

The class Exception is definitely a checked exception though.

jverda at 2007-7-29 17:08:57 > top of Java-index,Java Essentials,Java Programming...
# 5

Thanks for your responses, guys!

But I think I wasn't clear enough...

So, simplified question:

IOException is a checked exception.

Exception is a checked exception.

This code doesn't compiles (that's normal, because the IOException is never thrown in the try block):try {}

catch (IOException x) {}

This code compiles (that's strange, because Exception is never thrown in the try block!!):

try {}

catch (Exception x) {}

Any idea?

Cedric_acca at 2007-7-29 17:08:57 > top of Java-index,Java Essentials,Java Programming...
# 6

As we already said:

Catching any exception means you're also catching all subclasses of that exception.

IOE is checked, and all of its subclasses are checked.

Exception is checked, but its child RuntimeException (and all its descendants) is unchecked.

Because unchecked exceptions can be thrown anywhere, the compiler can't tell that nothing will be caught by catch (Exception exc). For instance, if IllegalArgumentException (unchecked descendant of Exception) is thrown, it will be caught there, and the compiler can't know whether it can be thrown or not.

jverda at 2007-7-29 17:08:57 > top of Java-index,Java Essentials,Java Programming...
# 7

Ok, I understand now.

Thanks again for your help!

Cedric_acca at 2007-7-29 17:08:57 > top of Java-index,Java Essentials,Java Programming...