• 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

empty try block

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens if the try block dosenot have any code in it?? is it legal and then is the catch block never accessable?? and wat abt the finally block??
u guies are the only ones i can come to ..need a way out....
cheers
nik
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens when you code, compile and run, such a try block?
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


u guies are the only ones i can come to ..need a way out....


Actually, we are not the only ones with the answer. The compiler is a much more reliable source than any "expert" since it has the final say of what can or cannot be done.

Layne
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try block will work fine if nothing is in it .

You cannot use any checked exception in a catch block if it is never thrown.
so with the empty try block u can use catch with unchecked exceptions if exception is thrown or not but u can use checked exception only if it is thrown.

Checked exceptions r those checked by compiler
unchecked exceptions r those which r not checked by compiler for eg arithmetic exception.
arithmetic exceptions r considered programers problem n is not checked by compiler.a divide by zero for eg shud b checked by ur own code

Finaly will always execute irrespective of whether exception is thrown or not.
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You cannot use any checked exception in a catch block if it is never thrown. so with the empty try block u can use catch with unchecked exceptions if exception is thrown or not but u can use checked exception only if it is thrown.



Huh? The try block is empty! Nothing will ever be thrown!
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Hentay Duke:


Huh? The try block is empty! Nothing will ever be thrown!



Rajesh is right.
compiles okay.causes a compilation error
 
Hentay Duke
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the reason for an empty try block?

I wasn't questioning what Rajesh is saying about caught and uncaught exceptions. I was just wondering why we care about the catch block after an empty try block. In my very limited experience I can't think of a reason to use an empty try block and therefore a reason to care about the resulting catch block. But I'm more than willing to be enlightened if you can explain.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree that there is no reason to do it, but it's legal.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic