aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why Compiler Error. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why Compiler Error." Watch "Why Compiler Error." New topic
Author

Why Compiler Error.

Shiaber Shaam
Ranch Hand

Joined: Jun 16, 2006
Posts: 252
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");
}
}


$Hi/\bEr
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Dustin Johnson
Greenhorn

Joined: Nov 14, 2006
Posts: 22
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...


SCJA 86%<br />SCJP 5.0 98%<br />SCWCD 1.4 79%<br />SCBCD 5.0 83%<br />--------------------------------<br />"I drank what???" - Socrates
Shiaber Shaam
Ranch Hand

Joined: Jun 16, 2006
Posts: 252
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

Joined: Feb 19, 2007
Posts: 11
UNREACHABLE code since system.out.println is never executed since everytime you are throwing an Exception without any condition.
Javier Sanchez Cerrillo
Ranch Hand

Joined: Aug 02, 2006
Posts: 152
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.


SCJP 5.0 95%<br /> <br />The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge.
Mahendra Shelke
Greenhorn

Joined: May 08, 2006
Posts: 20
just fool the compiler



here the compiler will think that flag will be false sometime and will result in compilation ..
saied ims
Ranch Hand

Joined: Jun 21, 2005
Posts: 109
throw new RuntimeException();
System.out.prinln("After Exceptions")://compiler error un reachable
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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 ]
 
wood burning stoves
 
subject: Why Compiler Error.