在不返回错误值的情况下观察错误?更多信息在里面

Watch for errors without returning error values? More info inside

我在谷歌搜索时遇到问题,所以这里有一个解释:

我想一个接一个地执行另一个 class 的方法列表,并让我的所有错误处理像 try-catch 场景一样发生。

像这样:

try
{
    var thing1 = Worker.GetThing1(); //returns proper value, so continue
    var thing2 = Worker.GetThing2(); //throws error with message
    var thing3 = Worker.GetThing3(); //doesn't get done, stopped at 2
}
catch (ExceptionError errorMessage)
{
    MessageBox.Show(errorMessage);
}

函数看起来像:

function GetThing1()
{
    if (!success)
    {
        throw ExceptionError("this is an error message.");
    }
}

问题是我不知道如何从另一个 class 中抛出异常,甚至不知道是否可以这样做。

显然,这是一些严重的伪代码,所以如果我不够清楚,请告诉我。

我制作了一个快速控制台应用程序来说明这一点。抛出新错误的语法是 throw new Exception(message); 代码:

class Program
{
    static void Main(string[] args)
    {
        try
        {
            var thing1 = Program.GetThing1(); //returns proper value, so continue
            var thing2 = Program.GetThing2(); //throws error with message
            var thing3 = Program.GetThing3(); //doesn't get done, stopped at 2
        }
        catch (Exception errorMessage)
        {
            Console.WriteLine(errorMessage.Message);
        }

        Console.ReadKey();
    }

    private static bool GetThing1()
    {
        bool success = true;

        if (!success)
        {
            // This will NOT be displayed in the console.
            throw new Exception("GetThing1 Error -> Not supposed to see this in output...");
        }

        return success;
    }

    private static bool GetThing2()
    {
        bool success = false;

        if (!success)
        {
            // This WILL be displayed in the console.
            throw new Exception("GetThing2 Error -> Expected, this has to be thrown!!!");
        }

        return success;
    }

    private static bool GetThing3()
    {
        bool success = true;

        if (!success)
        {
            // This will NOT be displayed in the console.
            throw new Exception("GetThing3 Error - > Not supposed to see this in output...");
        }

        return false;
    }
}

对于说 'all looks fine' 的人,请测试您的陈述。另外,在.NET中没有标准的class叫ExceptionError,你可以使用Exceptionclass来捕获所有类型的异常(但是发布者说他正在使用伪代码,所以我们不能过多地强调小细节。

我认为您已经创建了自定义异常 class,如果您的 getthings 在 diffrunt 库中运行,那么您应该将此 class 移动到某个公共库,并且您应该引用这两个库。

感谢各位有见识的人,我与我的原始代码非常接近。这是我最终得到的结果:

class Program
{
    static void Main(string[] args)
    {
        var worker = new Worker();
        try
        {
            worker.GetThing1();
            worker.GetThing2();
            worker.GetThing3();
        }
        catch (Exception errorMessage)
        {
            Console.WriteLine(errorMessage.Message);
        }
        Console.ReadKey();
    }
}

class Worker
{
    public void GetThing1()
    {
        var success = true;
        if (!success)
        {
            throw new Exception("This is an error message!");
        }


    }
    public void GetThing2()
    {

        var success = false;
        if (!success)
        {
            throw new Exception("This is an error message!");
        }

    }
    public void GetThing3()
    {

        var success = true;
        if (!success)
        {
            throw new Exception("This is an error message!");
        }

    }
}

与试图抓住 if-else 中的所有内容相比,它更易于阅读且效率更高。谢谢!