File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes catch and try Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "catch and try" Watch "catch and try" New topic

catch and try

Jello Yao

Joined: Jan 05, 2003
Posts: 13
Is it legal for a source file which only have try and without any catch blocks?
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Have you tried?
IIRC, a try block needs to be followed by at least one catch *or* finally block, but I might be wrong...

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
cornel jew
Ranch Hand

Joined: Jul 13, 2002
Posts: 37
The best way is do some coding and let the compiler answer your question.
Robbie shi

Joined: Jan 05, 2003
Posts: 28
that is illegal~~
each try block should match a catch block
and the finalize block is optional
----------------------------- IDE tool : JawaBeginer
2.Java Jar tool : JavaJar
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Ilja recalled correctly. A try block must have an associated catch or finally block. So, the catch block is also optional.
As mentioned, the compiler is one of the best ways to discover such requirements of the language.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Raj Neets
Ranch Hand

Joined: Nov 12, 2002
Posts: 46
A try block must have either finally or catch block or both .Else it will give compile time error.
I agree. Here's the link:
subject: catch and try
It's not a secret anymore!