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 empty try block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "empty try block" Watch "empty try block" New topic
Author

empty try block

nik rb
Greenhorn

Joined: Sep 25, 2005
Posts: 21
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

Joined: Aug 03, 2002
Posts: 7729
What happens when you code, compile and run, such a try block?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061

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


Java API Documentation
The Java Tutorial
Rajesh Chandra
Ranch Hand

Joined: Jun 13, 2005
Posts: 55
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

Joined: Oct 27, 2004
Posts: 198
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

Joined: Aug 05, 2005
Posts: 3742
    
  16
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


Joanne
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
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

Joined: Jul 11, 2001
Posts: 14112
I agree that there is no reason to do it, but it's legal.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: empty try block