2016-04-06 2 views
1

Ich habe so etwas wie dies:Rethrow eine Ausnahme dieser Fall

public byte[] AnyMethod(){ 

    try { 
    ... 
    } 
    catch (Exception e) { 
    string errorMessage = 
     "Some custom message, which should the caller of that method should receive"; 

    // I thought something of this ,to pass through my custom exception to the caller?! 
    throw new ApplicationException(errorMessage); 

    //but this does not allow the method 
    } 

} 

Aber:

Eine Ausnahme vom Typ ‚System.ApplicationException:

throw new ApplicationException(errorMessage); 

in führen 'trat in ... dll auf, wurde aber nicht im Benutzercode

behandelt

Wie erteile ich dem Aufrufer meiner oben genannten Methode die benutzerdefinierte Errror-Nachricht?

+0

Das ist, was Sie in VS Ausgabefenster sehen, aber ** mach dir keine Sorgen **, wenn Sie diese Ausnahme fangen, erhalten Sie Nachricht, die Sie zur Verfügung gestellt. BTW ... Ich hoffe, dass echtes Programm anders ist (Sie fangen generische Ausnahme, Sie fallen ursprünglichen Aufruf-Stack, Sie fallen ursprüngliche Ausnahme, Sie werfen einen nutzlosen generischen Ausnahme-Typ ...) –

+0

Ist dies ein Webservice, winforms, Konsole, oder was? – Fabjan

+0

@Fabjan Eine Klassenbibliothek, die für verschiedene Plattformen verwendet wird – kkkk00999

Antwort

1

zuerst über eine benutzerdefinierte Ausnahme oder zumindest eine sinnvollere anstelle von ApplicationException. Zweitens müssen Sie die Ausnahme abfangen, wenn Ihre Methode sie auslöst.

So sollte die Rufmethode auch den Methodenaufruf wickelt in einem try...catch:

try 
{ 
    byte[] result = AnyMethod(); 
}catch(MyCustomException ex) 
{ 
    // here you can access all properties of this exception, you could also add new properties 
    Console.WriteLine(ex.Message); 
} 
catch(Exception otherEx) 
{ 
    // all other exceptions, do something useful like logging here 
    throw; // better than throw otherEx since it keeps the original stacktrace 
} 

Hier ist eine Zusammenfassung, vereinfachtes Beispiel:

public class MyCustomException : Exception 
{ 
    public MyCustomException(string msg) : base(msg) 
    { 
    } 
} 

public byte[] AnyMethod() 
{ 
    try 
    { 
     return GetBytes(); // exception possible 
    } 
    catch (Exception e) 
    { 
     string errorMessage = "Some custom message, which should the caller of that method should receive"; 
     throw new MyCustomException(errorMessage); 
    } 
} 

Aber beachten Sie, dass Sie keine Ausnahmen für normales Programm verwenden sollten fließen. Stattdessen könnten Sie entweder true oder false zurückgeben, um anzugeben, ob die Aktion erfolgreich war, oder für die byte[] wie int.TryParse (oder die anderen TryParse Methoden) verwenden.

+0

@Fabjan: hat einen Kommentar hinzugefügt –

-1
publy byte[] AnyMethod(){ 

try{ 


}catch(Exception e){ 

    string errorMessage = string.Format("Some custom message, which should the caller of that method should receive. {0}", e); 

    //I thought something of this ,to pass through my custom exception to the caller?! 
    throw new ApplicationException(errorMessage); 
    //but this does not allow the method 
    } 

    } 

OR

public byte[] AnyMethod(){ 

try{ 


}catch(Exception e){ 

string errorMessage = "Some custom message, which should the caller of that method should receive"; 

//I thought something of this ,to pass through my custom exception to the caller?! 
throw new ApplicationException(errorMessage, e); 
//but this does not allow the method 
} 

} 
Verwandte Themen