【问题标题】:Custom Exception in C# appear in throw statement . i want it to be in most outer statement which generate exceptionC# 中的自定义异常出现在 throw 语句中。我希望它出现在产生异常的最外部语句中
【发布时间】:2016-09-22 10:11:55
【问题描述】:

我正在使用 Visual Studio 2013 进行 C# 编程。我正在测试自定义异常,但异常消息出现在 throw 语句中(这是正确的),但我希望它出现在导致异常 1 的行中?我该怎么做?

class Abc
{
    static void Main(String[] s)
    {
        Something(); // I want exception popup msg in this line
    }

    public static void something()
    {
        if(condition)
        {
            throw new SomeException(); // Exception popup message appear here
        }
    }
}

【问题讨论】:

  • “我希望它出现在导致第一个异常的行中”对我意味着什么,这非常不清楚。请尝试澄清,并理想地格式化您的代码下次更易读。 (也许您只需要在您的 Main 方法中添加一个 catch 块?)
  • 你需要先定义这个“出现”这个词。我想你需要的是堆栈跟踪。 - msdn.microsoft.com/en-us/library/…

标签: c# exception runtime throw


【解决方案1】:

我假设弹出窗口是指未捕获异常的 Visual Studio 异常弹出窗口。而你只是不想知道某个东西在哪里被调用。 您必须检查异常中的调用堆栈。它会告诉你 something() 被调用的地方。

在异常弹出窗口中单击查看详细信息以查看异常对象。 在调用堆栈中,您将看到如下内容: 抛出了一些异常: SomeFile.cs 12 中的 Something() 文件 someFile.cs 中的 Main() 5

45 是引发异常的行,12 是调用某些东西的行。

有关调用堆栈窗口的更多信息:https://msdn.microsoft.com/en-us/library/a3694ts5.aspx

【讨论】:

  • 怎么做?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-19
  • 1970-01-01
  • 1970-01-01
  • 2021-03-31
  • 1970-01-01
  • 2015-12-19
相关资源
最近更新 更多