Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

empty try block

 
nik rb
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens when you code, compile and run, such a try block?
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • 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
 
Rajesh Chandra
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • 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.
 
Hentay Duke
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • 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!
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree that there is no reason to do it, but it's legal.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic