1

I have an exception related issue

i have a class A, class B when i call some method of class B from class A which is put in pair with try catch final block then what happend when there come an exception in try block of Class A. then In those next steps after calling the mehod of class B, there comes an exception too, but it displays the recent exception i mean it overwrite the first exception of class B's methods m2(). And i remain unaware of actual exception that comes first.

Class A { try{ B b=new B(); b.m1(); b.m2(); } catch(Exception ex) // in catch block here what happens it display the b.m2() exception not the b.m1() exception, while i was thinking it should display first exception when it is calld at m1(); Why so ? { throw; } finally{} } class B { try { m1(){}; //here comes exception m2(){}; // it also throw some exception } catch(Exception ex) { throw; } finally { } } 
2
  • What language is this? You should add a tag to indicate the language. Also, you ought to indent the code properly. Commented Mar 3, 2011 at 10:15
  • It' asp.net, i post more code in edit part after a while. Commented Mar 3, 2011 at 10:23

3 Answers 3

2
try{ B b=new B(); b.m1(); b.m2(); } 

If m1 throws an exception, m2 is never executed. It is thus impossible that the catch statement shows the exception that m2 throws, if m1 has already thrown an exception.

Sign up to request clarification or add additional context in comments.

1 Comment

thats very weird, i am in same predicament how is it happening, i post more code soon
1

When there occurs an (uncatched) exception in method m1, then m2 will never get called. To find out, why it wont work in your case more information is needed. Its just impossible, that m1 throw an exception in your example.

I made a simular example like yours, which shows the expected behaviour:

public class ExceptionCatchExample { public static void main( String[] args ) { new ExceptionCatchExample(); } public ExceptionCatchExample() { Controller c = new Controller(); try { c.doMethod1(); c.doMethod2(); } catch ( Exception ex ) { System.out.println( " Error: " + ex.getMessage() ); } } } class Controller { public void doMethod1() throws Exception { System.out.println( "doMethod1()" ); throw new Exception( "exception in doMethod1()" ); } public void doMethod2() throws Exception { System.out.println( "doMethod2()" ); throw new Exception( "exception in doMethod2()" ); } } 

Comments

1

We need a little more info. First of all, which language are you using? Can you show us how m1() and m2() are implemented? Like @Sjoerd said, m2() will not execute if the try block containing m1 and m2 catches the exception in m1.

For example, in Java, try this code:

public class A { public void foo() { try { B b = new B(); b.m1(); b.m2(); } catch(Exception e) { e.printStackTrace(); } finally { // Do something } } } public class B { public void m1() throws Exception { throw new Exception("m1 exception"); } public void m2() throws Exception { throw new Exception("m2 exception"); } } public class Test { public static void main(String[] args) { // TODO Auto-generated method stub A a = new A(); a.foo(); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.