File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Exception  Handling -try Catch Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception  Handling -try Catch" Watch "Exception  Handling -try Catch" New topic

Exception Handling -try Catch

Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278
I have a Query regarding try-catch-finally clauses.

In most of the books ,its mentioned that try must have ,catch or finally block .

try can work without catch with finally .

But i tried some programs ,i observed ,For checked Exceptions ,
try must have catch ,even if finally is there ,else Compiler errors.

but in case of unchcked expcetions,lik RuntimeException ,catch can be omitted,Reason is obvious .JVM handles it but not to Checked case.

My question is : Is that so ,am i getting right ,If its right ,its not mentioned anywhere .Only thing book'-people write is about try-catch or finally thing .

i wirte here a code template to make myself more clear

{ throw new Exception(); //checked
}catch(Exception e){} //if omitted .compiler error

but for Runtime /unchecked case:

throw new RunTimeException(); //works without catch,

Sasikanth Malladi
Ranch Hand

Joined: Nov 04, 2000
Posts: 175
Try this:
change the Exception in your example code to ArrayIndexOutOfBoundsException (or something else that inherits from java.lang.RuntimeException).

Now notice the difference between the two example codes. Can you spot a difference? For example the inheritance hierarchy? Does the compiler still complain?

How about you change it back to Exception or IOException? Does the compiler still complain? Why? Why not?

Why does one work while the other gives a compile error?

Can you post a reply with your understanding of the problem now?

Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278
hi sashikanth
i tried my program according to your suggestions .
Same thing happens ,there is difference with -2 categories
With ArrayIndexOutOBounds (Unchked) -no Compile time error comes.
With IOException(Checed),errors comes ,so when catch added it works .
Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278
yes ,i have tried many other examples -IOException,MalformedURLException .
I hv given one in my Ques.
This is one more :
import*;class ExceptionDemo1

public static void main(String args[]){

URL url=new URL("");
catch(Exception e)


Actually ,my query is: try -finally thing doesnt work with Checked exceptions,but works for Unchecked.
But This thing not mentioned anywhere .Only thing i could find is :try-catch or finally
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
The exception does not have to be explicitly caught in the method that may throw it. That is if you declare the checked exception to be thrown by a particular method, you don't have to catch it there.

[ April 17, 2007: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
I agree. Here's the link:
subject: Exception Handling -try Catch
It's not a secret anymore!