获取手动抛出异常的消息



我有意为特定场景抛出异常,但我希望隐式地获得字符串格式的错误消息。我知道以下异常的重载之一是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
    }
}

你的意思是:?

try
{
    throw new FileLoadException
               ("File already compressed. Unzip the file and try again.");
}
catch (Exception ex)
{
    errMsg = ex.GetBaseException().Message;
}

您无法访问字符串THERE,我在这里解释一下:

string errMsg;
private void Compress()
{
    if (sourcePath.EndsWith(".zip"))
    {
        throw new FileLoadException
                   ("File already compressed. Unzip the file and try again.");
        // The following line is unreachable as the throw ends the function and gets the exception to the calling function as there is no try catch block to catch the exception.
        errMsg = //I want the above string here
    }
}

一种可能性是try/catch,即要设置变量的方法中的异常:

private void Compress()
{
    if (sourcePath.EndsWith(".zip"))
    {
        try
        {
            throw new FileLoadException
                       ("File already compressed. Unzip the file and try again.");
        }
        catch (Exception e)
        {
            errMsg = e.Message;
        }
    }
}

或者在调用方法中捕获异常:

try
{
    Compress();
}
catch (Exception e)
{
}

不管使用什么方法,e.Message都会以字符串的形式向您提供异常消息。

没有必要尝试捕获异常并设置消息。除非你再扔

try
{
    throw new FileLoadException("File already compressed. Unzip the file and try again.");
}
catch (Exception ex)
{
    errMsg = ex.GetBaseException().Message;
    throw;
}

我宁愿做这个

private void Compress()
{
    if (sourcePath.EndsWith(".zip"))
    {
        errMsg = "File already compressed. Unzip the file and try again.";
        return;
    }
}

不确定我是否100%理解,但如果您想要来自该异常的消息,您可以捕获它并检查exception.message

        try
        {
            throw new FileLoadException("Custom error string");
        }
        catch (Exception exception)
        {
            Console.WriteLine(exception.Message);
        }

这里应该有一个适合您的解决方案:)

        if (sourcePath.EndsWith(".zip"))
        {
            FileLoadException ex = new FileLoadException("File already compressed. Unzip the file and try again.");
            errMsg = ex.ToString();
        }
        Console.WriteLine(errMsg);

为了打破你的破例,我会做这样的

    static string sourcePath = "test.zip"; //hardcoded file
    static string errMsg; //the error string
    private static void Compress()
    {
        try
        {
            if (!sourcePath.EndsWith(".zip"))
            {
                Console.WriteLine("File doesn't end with zip so it can be compressed"); //if it does not end with zip its rdy for compressing and here you can indput your code to compress
            }
            else
            {
                throw new Exception(); //instead of using try catch you can also generate the code in here instead of throwing an exception.
                                       //when throwing a new exception you make it stop the if setting and jump into the catch if you use break it wont enter the catch but instead it will just jump over it.
            }
        }
        catch 
        {
            //Here it will generate the custom exception you wanted and put it inside the errMsg
            errMsg = new FileLoadException("File already compressed. Unzip the file and try again.").ToString();
            Console.WriteLine(errMsg);
        }

ofc这是在控制台中完成的,这就是我使用console.writeline的原因。您可以将这些更改为

最新更新