• 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

when will the exception will be caught ???

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

jus look at the code....

import java.io.*;
class Test {
public int myMethod1() throws EOFException {
return myMethod2();
}
public int myMethod2() throws EOFException {
// Some code that actually throws the exception goes here
return 1;
}
}

here myMethod2 throws the End of file exception which is a checked exception ,also myMethod1 which called the method2 throws the exception ...
so my doubt is when will the exception b caught ...(i.e where will b the catch block b)

and also how to handle the runtime exception......

thanks & regards

srikanth reddy
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The catch block will be wherever you put it. Otherwise the program will crash upon throwing the exception.
 
srikanth reddy
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry i didnt get u ....

i have two doubts regarding the above code ..

1)i have tried compiling the above code it says statement not reachable
i.e after throwing an exception it doesnt return 1...so can i say that the
complier dont continue with the rest of the code(after the exception) like
the "continue".

2)in the code no method is catching the exception (all throws the exception)even the calling method also .is it right ???

please can u clarify quickly so that i can move fwd with my preparation .

thanks & regards

srikanth
 
Sergei Iakhnin
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since no code explicitly catches the exception the default exception handling mechanism will be employed which will result in program termination and printout of the stack trace.

When a checked exception occurs it needs to be handled in order for normal execution to resume. It is not right to only throw the exception and never catch it unless you are satisfied with the default exception handling behaviour.
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



1)i have tried compiling the above code it says statement not reachable
i.e after throwing an exception it doesnt return 1...so can i say that the
complier dont continue with the rest of the code(after the exception) like
the "continue".



Once a excception is ocurred it should be catched and if it satisfies the condition for continuing, then execution flow will continue

In the above code if use try & catch block use return statement in both the blocks otherwise have it in finally block

Then code will compile


in the code no method is catching the exception (all throws the exception)even the calling method also .is it right ???



yes, we can have like that then compiler handles it.(ensure no return statements in it] if so we should use try along with catch or finally blocks

hope it clears
 
author
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any method that calls checked-exception-throwing methods must deal with the exception in one of two ways:

1) The method declares that it throws the exception type. That's what your myMethod1() does: it calls myMethod2(), which throws EOFException. Since myMethod1() also throws EOFException, there's no problem. Until you actually want to call myMethod1() somewhere.

2) The call to the exception-throwing method must appear in a "try" block, with appropriate other stuff. For example:

 
Don't MAKE me come back there with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic