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

try block

 
Jimmy Bonds
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 7783
62
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jimmy Bonds
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Junilu Lacar
Bartender
Pie
Posts: 7783
62
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 7783
62
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic