aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes empty try block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "empty try block" Watch "empty try block" New topic
Author

empty try block

chetana kumar
Greenhorn

Joined: Aug 22, 2006
Posts: 4
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: 235
You can have that, provided that the exception is some unchecked exception.
Brian Mozhdehi
Ranch Hand

Joined: Aug 17, 2006
Posts: 81
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

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

Naseem


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
It wont throw a compiler issue, i.e. it will compile.

Wrong. This code produces a compiler error:


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18978
    
  40

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.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Stary Kapec
Ranch Hand

Joined: Dec 04, 2005
Posts: 81

Throwable, Error, Exception all are unchecked.




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

Naseem
 
 
subject: empty try block