static void Main() { Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException); Application.Run(new MainForm()); } static void Application_ThreadException(object sender, ThreadExceptionEventArgs e) { MessageBox.Show("An error occurred: " + e.GetException().Message); }
protected void Application_Error(object sender, EventArgs e) { Exception exception = Server.GetLastError().GetBaseException(); Logger.LogException(exception); Response.Redirect("~/Error.aspx"); }In both examples, the GetException method is used to retrieve the exception raised and perform additional actions, such as logging or displaying the error to the user.