• 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

why the statements after throw statement will not be executed.

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A{
void method(){
try{
throw new Exception();
System.out.println("hello java");
}
catch(Exception e){
}
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It goes to the 'Catch' immediately while finding exception, and then executes what's after the 'Catch' block.
[ December 12, 2007: Message edited by: Cooky Lovy ]
 
Greenhorn
Posts: 16
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code you have posted will probably show an error like "code unreachable".

You only use throw when you want to specify a restriction to the application you're working on. For example, in an employee information manager, an employee cannot specify his dependents to be two spouses... Thats a restriction that you want your business logic to handle. Thats when you use throw. And anything after throw statement will not execute.
 
Ranch Hand
Posts: 49
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you say throw new Exception() the control goes to the Catch block where you can give your output message.

After your catch block is executed, the control goes to the next statement after your try and catch block, so now try executing the below block it would print the "hello java"

class A{

void method(){
try{
throw new Exception();
}catch(Exception e){
}
System.out.println("hello java");
}//end of method
} //end of Class A

try reading http://java.sun.com/docs/books/tutorial/essential/exceptions/throwing.html
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some people complain that throw is a thinly disguised GOTO. That may help explain why it can jump over statements, and even jump back up the call stack.

Throw can be like GOTO in a bad way if you use it for normal program logic. It really should indicate an exceptional condition, one that is not expected in normal processing. In that light, I'd argue against the "business restriction" definition above unless we discover two spouses via a duplicate key error in a database. There is room for some debate about what is "business" and what is "exceptional", but for sure: don't use throw as an alternative to if-then-else.
 
reply
    Bookmark Topic Watch Topic
  • New Topic