Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception handling

 
Sasha Hernandez
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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()?
 
Igor Ko
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • 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 ]
 
Sandeep Jindal
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • 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.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic