Question

Est-il possible, comment faire exception actuellement lancée (si elle existe)?

Je voudrais réduire quantité de code et d'appliquer une réutilisation pour la tâche ressemble à:

Exception thrownException = null;
try {
    // some code with 3rd party classes, which can throw unexpected exceptions
}
catch( Exception exc ) {
    thrownException = exc;
    LogException( exc );
}
finally {
    if ( null == thrownException ) {
        // some code
    }
    else {
        // some code
    }
}

et le remplacer par ce code:

using( ExceptionHelper.LogException() ) {
    // some code with 3rd party classes, which can throw unexpected exceptions
}
using( new ExceptionHelper { ExceptionAction = ()=> /*some cleaning code*/ } ) {
    // some code with 3rd party classes, which can throw unexpected exceptions
}

public class ExceptiohHelper : IDisposable {
    public static ExceptionHelper LogException() {
        return new ExceptionHelper();
    }

    public Action SuccessfulAction {get; set;}
    public Action ExceptionAction {get; set;}

    public void Dispose() {
        Action action;
        Exception thrownException = TheMethodIDontKnow();
        if ( null != thrownException ) {
            LogException( thrownException );
            action = this.ExceptionAction;
        }
        else {
            action = this.SuccessfulAction;
        }

        if ( null != action ) {
            action();
        }
    }
}

Est-ce scénario posible?

Merci

Était-ce utile?

La solution

Que pensez-vous de ce qui suit. Au lieu de regarder le problème « Comment obtenir la dernière exception? », Si vous changez à, « Comment puis-je exécuter un morceau de code avec un peu plus de contrôle? »

Par exemple: Au lieu d'un ExceptionHelper vous pourriez avoir un ActionRunner.

public class ActionRunner
{
    public Action AttemptAction { get; set; }
    public Action SuccessfulAction { get; set; }
    public Action ExceptionAction { get; set; }

    public void RunAction()
    {
        try
        {
            AttemptAction();
            SuccessfulAction();
        }
        catch (Exception ex)
        {
            LogException(ex);
            ExceptionAction();
        }
    }

    private void LogException(Exception thrownException) { /* log here... */ }
}

Il aurait au moins vous donner une réutilisation du SuccessfulAction et ExceptionAction en supposant que le AttemptAction varie entre les appels.

var actionRunner = new ActionRunner
{
    AttemptAction = () =>
    {
        Console.WriteLine("Going to throw...");
        throw new Exception("Just throwing");
    },
    ExceptionAction = () => Console.WriteLine("ExceptionAction"),
    SuccessfulAction = () => Console.WriteLine("SuccessfulAction"),
};
actionRunner.RunAction();

actionRunner.AttemptAction = () => Console.WriteLine("Running some other code...");
actionRunner.RunAction();

Autres conseils

L'idée est que vous gérez des exceptions dans le bloc catch ...

Cela dit, une exception est un type de référence, donc vous pouvez toujours déclarer une variable d'exception en dehors du champ d'essai ...

Exception dontDoThis;
try
{
    foo.DoSomething();
}
catch(Exception e)
{
    dontDoThis = e;
}
finally
{
    // use dontDoThis...
}

Si vous cherchez à attraper des exceptions inattendues, vous devriez manutentionneront le

scroll top