2011-03-03 2 views
0

J'ai une exception question liéeessai, problème lié à l'exception des prises

i ont une classe A, classe B quand j'appelle une méthode de classe B de la classe A qui est mis en paire avec prises d'essai bloc final Ensuite, que se passe-t-il quand il y a une exception dans try block de classe A. puis Dans les prochaines étapes après avoir appelé le mehod de classe B, il y a aussi une exception, mais il affiche l'exception récente je veux dire qu'il écrase la première exception des méthodes de classe B m2(). Et je ne suis pas au courant de l'exception réelle qui vient en premier.

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 
    { 
    } 
} 
+0

Quelle langue est-ce? Vous devriez ajouter une étiquette pour indiquer la langue. En outre, vous devez indenter le code correctement. –

+0

It 'asp.net, je poste plus de code dans la partie d'édition après un certain temps. – NoviceToDotNet

Répondre

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

Si une exception m1 lancers francs, m2 est jamais exécutée. Il est donc impossible que l'instruction catch montre l'exception que m2 lance, si m1 a déjà levé une exception.

+0

c'est très bizarre, je suis dans la même situation comment ça se passe, je poste plus de code bientôt – NoviceToDotNet

1

Nous avons besoin d'un peu plus d'informations. Tout d'abord, quelle langue utilisez-vous? Pouvez-vous nous montrer comment sont implémentés m1() et m2()? Comme @Sjoerd a dit, m2() ne s'exécutera pas si le bloc try contenant m1 et m2 attrape l'exception dans m1.

Par exemple, en Java, essayez ce 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(); 
    } 

} 
1

Quand il se produit un (uncatched) exception dans la méthode m1, alors m2 rien ne sera jamais appelé. Pour savoir, pourquoi cela ne fonctionnera pas dans votre cas plus d'informations sont nécessaires. C'est juste impossible, que m1 jeter une exception dans votre exemple.

J'ai fait un exemple Simular comme le vôtre, qui montre le comportement attendu:

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