• 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 handling

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suppose you had the following code:
method_A();
try{
method_B();
method_C();
}
catch(Exception)
{
}
method_D();
If method_A() were to throw an exception and the catch block handled it, would program execution continue with the statements in the try block or would it continue after the catch block?
If method_B() were to throw an exception and the catch block handled it, would program execution continue with method_C() or with method_D()?
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q1 exception will be thrown, but there isn't try/catch for it in
your scrap of code.
Q2 D.
[ December 07, 2003: Message edited by: Igor Ko ]
 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Sasha,
I think you are confused with the try and catch block.
The simple concept of exception handling is that

The code outside the try block is never goin to be catched. If it throws the exception, defalt exception handling with catch that. That means try is for telling the java to do something different(catch), if the code in try makes some exception.
Second point for exception handling is if there is some exception at some code in try block, catch will handle it and the flow goes after catch statement. That means if there is some error in "some-code1", catch will run and then the outside-code1.
So answer for ur questions is
1) If method_A() were to throw an exception, the default handler will catch it, the program will stop and nothing will run after that.
2)If method_B() were to throw an exception and the catch block handled it, program execution continue with method_D()

Hope this is clear.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sasha, perhaps the best answers to such questions are the ones that you discover for yourself by writing an example program or two, and seeing what happens.
Can you write a program to discover the correct answers?
[ December 08, 2003: Message edited by: Dirk Schreckmann ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic