• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Bear Bibeault
  • paul wheaton
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Mikalai Zaikin
  • Piet Souris
Bartenders:

Why Compiler Error.

 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");
}
}
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
UNREACHABLE code since system.out.println is never executed since everytime you are throwing an Exception without any condition.
 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ..
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
throw new RuntimeException();
System.out.prinln("After Exceptions")://compiler error un reachable
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
I will suppress my every urge. But not this shameless plug:
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic