开发者

try, catch exception related problem

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 dis开发者_开发百科play 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
    {
    }
}


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.


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()" );
  }
}


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();
    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜