gpt4 book ai didi

C#转换错误

转载 作者:行者123 更新时间:2023-11-30 16:27:13 25 4
gpt4 key购买 nike

我从下面显示的代码中得到以下错误,并且找不到正确转换对象的方法。非常感谢任何帮助。

错误发生在ex = e.ExceptionObject;行。

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();
}
}

最佳答案

Exception ex = (Exception)e.ExceptionObject;

关于C#转换错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8096134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com