aspose file tools*
The moose likes Beginning Java and the fly likes 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 "try block" Watch "try block" New topic
Author

try block

Jimmy Bonds
Ranch Hand

Joined: Jun 05, 2001
Posts: 38
Hi All-
Question:
If the statement contained in a try block does not throw an exception the statement in the try block will just be executed.
True or False?
Up until now I thought it was true.
because the following simple code:
class class1
{
public static void main(String [] args)
{
int[]x={1,2,3,4,5};
try
{
System.out.println(x[4]);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("got it");
}
}
}
//end
this will display: 5
so since an exception was not thrown in the code inside the try block the code itself was executed.
However, playing with some code tonite:
public class class2
{
public static void main(String [] args)
{
asdf hue = new asdf();

try
{
hue.method1();
}
catch(bbbException e)
{
System.out.println("got it");
}
}
}
Compiling this code will generate a message error saying that exception bbbException is never thrown in the body of the try statement. An ArrayIndexOutOfBoundsException was never thrown in the first try statement in class1 yet it still executed code in the try statement. Am I missing something here? Or is it just something so obvious I should smack myself.
Thanks
Jimmy-
------------------
yah, yah I'm a newbie
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5018
    
    8

Does bbbException extend Exception rather than RuntimeException? If it does, then that makes bbbException a checked exception, which makes the compiler enforce stricter rules than with unchecked exceptions. One of these rules is that if you declare a catch block for a checked exception, then the associated try block should have a statement that can throw the checked exception.

Junilu - [How to Ask Questions] [How to Answer Questions]
Jimmy Bonds
Ranch Hand

Joined: Jun 05, 2001
Posts: 38
Cool-
Thanks Junilu,
bbbException did extend Exception. I changed it to RuntimeException and it worked fine. Thanks for the tip!
Jimmy-
----------------
yah, yah I'm a newbie
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
One of these rules is that if you declare a catch block for a checked exception, then the associated try block should have a statement that can throw the checked exception.
I think that the compiler complains only because hue.method1() doesn�t have the throws clause with bbbException in it. Otherwise an attempt to catch a checked exception that is never thrown is not illegal. For example this code

compiles and runs fine.

Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5018
    
    8

hmmmm... then why doesn't this compile?:
<pre>
import java.io.*;
public class Test {
void thisChokes() {
try {}
catch (IOException e)
{
System.out.println("See?");
}
}
}

**************
D:\java2\junk\Test.java:8: exception java.io.IOException is never thrown in body of corresponding try statement
catch (IOException e) {
^
1 error
</pre>

From JLS �11.5: "The subclasses of Exception other than RuntimeException are all checked exception classes."
I interpret this to mean that Exception is not itself a checked exception. This would explain why Map's code compiles successfully. It also makes sense because if you catch Exception, then you would catch both checked and unchecked exceptions.
That means that aside from RuntimeException and its subclasses and Error and its subclasses, for any checked exception caught in a catch block, there must be a statement in the corresponding try block that is capable of throwing that exception.
There was a thread about a similar issue a while back started by James Du.

[This message has been edited by JUNILU LACAR (edited June 22, 2001).]
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
Interesting... It seems to me that Exception objects are checked exceptions.
This code, for example

raises the compiler error that Exception must be either caught, or declared in the throws clause
This code

produces the error about incompatible throws clauses for overridden/overriding methods.
But you are right about the try-catch construct. Probably, as you said, the compiler doesn�t complain because Exception can catch RuntimeExceptions as well.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5018
    
    8

Maps,
I dug around a bit more and the JLS 11.2 seems to support that Exception is a checked exception.
It is possible that there's some compiler magic involved since Exception is treated like a checked exception in some cases and an unchecked exception in others. In the scenarios that we each showed it made sense for Exception to be treated one way or the other.
frank davis
Ranch Hand

Joined: Feb 12, 2001
Posts: 1479
The results were starting to favor the "Exception is a checked exception" camp so I'll give this example to balance things out :
class h {

void main(String s){
try {}
catch (Exception e){}
}
}
This compiles fine, whereas any other normal checked exception will not compile...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: try block