• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

throw

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
This compile without error.
public class TestThrow
{
public static void main(String[] args)throws Exception
{
try{
System.out.println("TestThrow");
throw new Exception("mistake");
}catch(Exception e){
System.out.println("TestThrow catch");
}
}
}
But this give compile error
public class TestThrow
{
public static void main(String[] args)throws Exception
{
try{
throw new Exception("mistake");
System.out.println("TestThrow");
}catch(Exception e){
System.out.println("TestThrow catch");
}
}
}
Why???
Thanks in advence.
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jordi
I assume your compile error is statement not reached. This is becasue you have a print statement after you throw your exception. As soon as the exception is thrown it stops executing the code in the try block and goes to the catch block(s) to try to find a catch statement for the exception that was thrown. To make it print "TestThrow" just put that line above the throw line.
hope that helps you out

Dave
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't run the example, but is it an unreachable code error?
If it is, the explanation is, once the exception is thrown (and it will always be thrown) the rest of the try block is ignored.
So, the code will not compile because the statement below the one that throws the exception will never be reached.
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did run the code and just as we expected, it was a "Statement not reached" compiler error.
April
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But in KAM book, on page 161,there's an example of same kind where try blk throws excpn at first line and catch is executed.
The explanation is also like THE REST OF THE TRY BLK IS NOT EXECUTED.
Then why compiler error in above code?
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Khalid's book there is an if condition before throwing the exception.
it is somewhat like this.

I think at compile time the compiler doesn't know whether the exception is going to be thrown or not. so it works.
Vanitha.
 
The two armies met. But instead of battle, they decided to eat some pie and contemplate this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic