Maak van een CLR Unhandled Exception een handled exception

Een heel enkele keer gaat er op een heel diep niveau toch iets mis in je applicatie. Wil op zo'n moment de gebruiker niet opzadelen met zo'n vreselijk CLR Unhandled Exception bericht?

Dat hoeft ook niet. De meeste exceptions kun je afvangen met een try/catch, maar met bijvoorbeeld ThreadException lukt dat niet.

Je kunt een eigen exception handler schrijven om alle unhandled exception toch netjes af te vangen.

Zie hieronder voor een voorbeeld.

 

//The Error Handler class
//We need a class because event handling methods can't be static
internal class CustomExceptionHandler {

    //Handle the exception event
    public void OnThreadException(object sender, ThreadExceptionEventArgs t) {

        DialogResult result = DialogResult.Cancel;
        try {
            result = this.ShowThreadExceptionDialog(t.Exception);
        }
        catch {
            try {
                MessageBox.Show("Fatal Error",
                                "Fatal Error",
                                MessageBoxButtons.AbortRetryIgnore,
                                MessageBoxIcon.Stop);
            }
            finally {
                Application.Exit();
            }
        }

        if (result == DialogResult.Abort)
            Application.Exit();

    }

    //The simple dialog that is displayed when this class catches and exception
    private DialogResult ShowThreadExceptionDialog(Exception e) {
        string errorMsg = "An error occurred please contact the adminstrator with" +
                          " the following information:\n\n";
        errorMsg += e.Message + "\n\nStack Trace:\n" + e.StackTrace;
        return MessageBox.Show(errorMsg,
                                "Application Error",
                                MessageBoxButtons.AbortRetryIgnore,
                                MessageBoxIcon.Stop);
    }
}

....

//Register the custom error handler as soon as we can in Main
//to make sure that we catch as many exceptions as possible
public static void Main(string[] args) {
    CustomExceptionHandler eh = new CustomExceptionHandler();
    Application.ThreadException += new ThreadExceptionEventHandler(eh.OnThreadException);
    Application.Run(new ErrorHandler());
}

 

Bron: www.gotdotnet.com

 

Commentaar van anderen:
ChristianLouboutin op 14-8-2010 om 10:48
Christian Louboutin Shoes, Christian Louboutin, Christian Louboutin Shoes, Wedding Shoes, Christian Louboutin Copyright 2010, Chemicals Chemistry via VerticalNews. Christian Louboutin Shoes, Wedding Shoes Pattinson great actorly virtue is that he wears clothes well, so it too bad he slackered-out in cargo pants here. Christian Louboutin, Christian Louboutin Shoes, Wedding Shoes, Discount Christian Louboutin, Manolo Blahnik Shoes Tyler is less revealed than telegraphed through accessories a dead brother depth, a pack-a-day habit angst, a bookstore job smart, Discount Christian Louboutin, Louboutin, Christian Louboutin Sale, Louboutin Shoes, Sale Christian Louboutin Rodita zip sandals New style Black 14 a rich, aloof, and permanently disappointed daddy Pierce Brosnan. Louboutin Sale, Herve Leger Bandage Dress, Herve Leger Dress, Herve Leger V Neck Dress, Herve Leger Bandage Dress Falling for You Love, angst, and something else is in the air in Remember Me Remember Me Herve Leger Dress, Chanel Shoes, Yves Saint Laurent Shoes, Manolo Blahnik Shoes Platform Cage Sandal 13 by Allen Coulter Summit Entertainment Opens March 12 Putatively a new romance starring Robert Pattinson, Remember Me begins like a vigilante movie Alexander Wang Shoes, Louboutin Shoes, Louboutin Sale, Louboutin, Christian Louboutin Sale, Buy Christian Louboutin A Brooklyn subway platform, a racially charged stickup girl watches her mother get shot. Christian, Christian Louboutin Discount, Christian Dior Shoes, Christian Louboutin Pumps Pattinson great actorly virtue is that he wears clothes well, so it too bad he slackered-out in cargo pants here.
Geef feedback:

CAPTCHA image
Vul de bovenstaande code hieronder in
Verzend Commentaar