Wol*_*ish 2 c# exception-handling exception
我故意为特定场景抛出异常,但我会隐含地希望以字符串格式获取错误消息.我知道以下异常的一个重载是string message,但是如何访问该字符串?
这是相关的片段:
string errMsg;
private void Compress()
{
if (sourcePath.EndsWith(".zip"))
{
throw new FileLoadException
("File already compressed. Unzip the file and try again.");
errMsg = //I want the above string here
}
}
Run Code Online (Sandbox Code Playgroud)
你的意思是这个:?
try
{
throw new FileLoadException
("File already compressed. Unzip the file and try again.");
}
catch (Exception ex)
{
errMsg = ex.GetBaseException().Message;
}
Run Code Online (Sandbox Code Playgroud)