File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception  Handling -try Catch" Watch "Exception  Handling -try Catch" New topic
Author

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


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

but for Runtime /unchecked case:

try
{
throw new RunTimeException(); //works without catch,
}finally{}


thanks
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?

Sashi
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
Hi
yes ,i have tried many other examples -IOException,MalformedURLException .
I hv given one in my Ques.
This is one more :
import java.net.*;class ExceptionDemo1
{

public static void main(String args[]){

try{
URL url=new URL("http://yahoo.com");
}
catch(Exception e)
{System.out.println("caght");}
finally
{System.out.println("finally");}

}}

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
thnx
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Exception Handling -try Catch
 
Similar Threads
Velmurugan's notes
Crettaz's mock exam q46
EXCEPTION DILEMA
try {} body vs method body - inconsistency in compiler check for Exception being thrown
unreachable statement again