Forums Register Login

Exception Handling -try Catch

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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 .
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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 ]
Something must be done about this. Let's start by reading this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1035 times.
Similar Threads
Catching exception not thrown in try clause
is CloneNotSupportedException Checked Exception.
unreachable statement again
difference between JVM thrown exceptions and programmatically thrown exceptions?
Exception confusion
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2024 21:05:38.