This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why Compiler Error.

 
Shiaber Shaam
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Test_Exception
{

public static void main(String[] args)
{
try
{
throw new RuntimeException();
System.out.prinln("After Exceptions"):
}
catch(Exception e)
{
System.out.println("Exception");
}
finally
{
System.out.println("Finally");
}
System.out.println("TRY");
}
}
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use Code Tags when posting code.

I think the compiler error message is pretty clear: System.out.println("After Exceptions"); is an unreachable statement.
[ February 19, 2007: Message edited by: marc weber ]
 
Dustin Johnson
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compiler error because nothing is tried before you throw a RuntimeException? Plus you mispelled a word in there, see if you can find it Just kidding, of course you saw it afterwards I always do heh...
 
Shiaber Shaam
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My doubt is exactly like..
* Whenever the remining code in the try block after the code causing exception will get executed.

Thanks.
 
Ravi Pinnaboyina
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
UNREACHABLE code since system.out.println is never executed since everytime you are throwing an Exception without any condition.
 
Javier Sanchez Cerrillo
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are several syntax errors in the code you wrote. But I think you are asking about an "unreachable" statement error.

Java compilers are smart and they can detect some minor logic.(the following example is almost the same as yours):


Java compilers sometimes can detect "unreachable statements" before the code is executed. Like in the previous example.

This degree of detection has a level. What if you throw the same Exception but this time inside a method???. Lets change the code a bit. Both are logically equivalent.



Check it out.
 
Mahendra Shelke
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just fool the compiler



here the compiler will think that flag will be false sometime and will result in compilation ..
 
saied ims
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
throw new RuntimeException();
System.out.prinln("After Exceptions")://compiler error un reachable
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shiaber Shaam:
My doubt is exactly like..
* Whenever the remining code in the try block after the code causing exception will get executed...

In this example, never.

An exception says, "There's something wrong here that's preventing me from continuing with this code." So instead of trying to move forward, an exception is thrown and control is passed outside the try block. It's like taking an alternate route when a "Road Closed" sign unexpectedly appears on the path you're following.

The catch and finally blocks can provide a graceful exit so that the program doesn't just crash.

If you wanted to try the code again, you would need to recall the method from inside the catch or finally block. But if you're able to code a process for recovering from the exception, then this probably should have been built into the code in the first place, avoiding an exception entirely.

In your code, the exception is always thrown, so the "Road Closed" sign is not unexpected, and the compiler tells you that this path can never be taken. As suggested above, you could change this by making the exception a chance occurrence. For example, in the code below, an exception might or might not be thrown, depending on nextBoolean(). But each time an exception is thrown, the method is recalled. This continues until the code executes without an exception.

[ February 20, 2007: Message edited by: marc weber ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic