• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception Handling -try Catch

 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic