【问题标题】:Watch for errors without returning error values? More info inside监视错误而不返回错误值?里面有更多信息
【发布时间】:2016-03-02 23:16:02
【问题描述】:

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

我想一个接一个地执行另一个类的方法列表,并让我的所有错误处理都像 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.");
    }
}

问题是我不知道如何从另一个类抛出异常,或者是否可以这样做。

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

【问题讨论】:

  • 看起来不错。除了 Worker.GetThing1 会抛出你的例子。
  • 什么是ExceptionError
  • 一切看起来都很好......请详细说明您的疑问。或者什么不起作用
  • 如果您想在自定义消息中抛出异常,请使用new Exception("Your message"),否则您的代码看起来不错

标签: c# error-handling try-catch


【解决方案1】:

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

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 中的所有内容更容易阅读和更高效。谢谢!

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      我制作了一个快速控制台应用程序来说明这一点。抛出新错误的语法是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;
          }
      }
      

      对于那些说“一切看起来都很好”的人,请测试你的陈述。另外,.NET 中没有标准的ExceptionError 类,你可以使用Exception 类来捕获所有类型的异常(但发帖人说他使用的是伪代码,所以我们不能过多地锤击小细节)。

      【讨论】:

      • 为什么说“这不会显示在控制台中”
      • 可能是因为它不会。
      • 它不会显示在控制台中,因为 if 语句永远不会允许它。
      猜你喜欢
      • 1970-01-01
      • 2019-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多