Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# conversion error

Tags:

c#

I get the following error from the code shown below and can not find a way to cast the object properly. Any help greatly appreciated.

The error occurs on the ex = e.ExceptionObject; line.

Cannot implicitly convert type 'object' to 'System.Exception'. An explicit conversion exists (are you missing a cast?) (CS0266) - C:\DocmentMDB\DocumentMDB.ConvertedToC#\ErrorHandler.cs:59,9

public static void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs e)
{
    // handles all unhandled (i.e. no Catch block) exceptions
    // the following code must be placed in Sub Main and the project
    // use Sub Main as startup
    //Dim currentDomain As AppDomain = AppDomain.CurrentDomain
    //AddHandler currentDomain.UnhandledException, AddressOf myExceptionHandler

    Exception ex = null;
    ex = e.ExceptionObject;

    string strErrorMsg = null;
    string strDisplayMsg = null;
    string strPrintMsg = null;
    int intLoc = 0;

    strErrorMsg = "Unhandled Error In : " + strFormName + " - " + ex.TargetSite.Name + Constants.vbCrLf + Constants.vbCrLf + ex.Message;

    strPrintMsg = "Error detected: " + DateAndTime.Now + Constants.vbCrLf + Constants.vbCrLf + strErrorMsg + Constants.vbCrLf + Constants.vbCrLf + ex.StackTrace;

    strDisplayMsg = "Report this error to your System Administrator" + Constants.vbCrLf + Constants.vbCrLf + strErrorMsg + Constants.vbCrLf + Constants.vbCrLf + "Click YES to print this message.";

    if (MessageBox.Show(strDisplayMsg, "Unhandled Program Error", MessageBoxButtons.YesNo) == DialogResult.Yes) {
        // print the error message
        ErrPrint myPrintObject = new ErrPrint(strPrintMsg);
        myPrintObject.Print();
    }
}
like image 953
John Lee Avatar asked Mar 28 '26 23:03

John Lee


1 Answers

Exception ex = (Exception)e.ExceptionObject;
like image 135
Uwe Keim Avatar answered Apr 03 '26 17:04

Uwe Keim