what happens when you have a try block without any code folloewd by a catch block that catches some exception?
Surendra Kumar
Ranch Hand
Joined: Jul 04, 2006
Posts: 87
posted
0
You can have that, provided that the exception is some unchecked exception.
Brian Mozhdehi
Ranch Hand
Joined: Aug 17, 2006
Posts: 81
posted
0
Nothing happens. The catch block will only catch an exception thrown in the Try block. If nothing happens in the try block, then no exception could ever possibly be thrown and therefore whatever is in the catch block would never be executed.
It wont throw a compiler issue, i.e. it will compile. But its pointless to add such a thing to any application.
Stary Kapec
Ranch Hand
Joined: Dec 04, 2005
Posts: 81
posted
0
You can have that, provided that the exception is some unchecked exception.
e and t exceptions are also allowed. Are they also considered as checked exceptions?
Naseem Khan
Ranch Hand
Joined: Apr 25, 2005
Posts: 809
posted
0
Those classes which extend Exception is checked only with the exception of java.lang.RuntimeException class.
Al other is unchecked. e.g., Throwable, Error, Exception all are unchecked.
e and t exceptions are also allowed. Are they also considered as checked exceptions?
The compiler will not complain about any unchecked exception -- or any throwable class that an unchecked exception is an instance of. There are unchecked exceptions that are instance of the Exception and Throwable classes, hence, it will not complain.
But even though Exception is not checked, compiler still requires it to be checked :-) So why not to call it checked? The same is true for Throwable.
Naseem Khan
Ranch Hand
Joined: Apr 25, 2005
Posts: 809
posted
0
How the compiler will know which exception object is thrown from foo() method. It could be an instance of IOException which is checked so compiler will force you to catch it or declare it by throws clause.