• 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

exception

 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

why we can;t throw more than one excption in a try block as shown in below code.

public int method6() {

//throw new RuntimeException();
try{
throw new ArithmeticException();
throw new RuntimeException();
}
catch(RuntimeException re){
}
return 0;
}

but if we provide in the if else code then it will compiled
public int method6() throws Exception {

try{
int i = 1;
if (i==1){
throw new ArithmeticException(); }
else {

throw new RuntimeException();}
}
catch(RuntimeException re){
}
return 0;

}
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your first example the second throw clause will never be reached, so the compiler will complain about it being unreachable.

However, in the second example, there is a chance of either one of the throw clauses will execute (depends on the result of the expression in the if-stat), so the compiler won�t complain about it.
 
Beny Na
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot for your reply, it is make sense for me now.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic