【问题标题】:throw new exception("Test"); does not throw my text抛出新异常(“测试”);不抛出我的文字
【发布时间】:2017-07-06 09:13:27
【问题描述】:
throw new Exception("Test");

仅抛出

我无法在其中获取我的文本...

我也试过了

throw new ArgumentException("Test");

带有try catch 和不带。

但正是我的书面投掷使窗户成为现实。

编辑: 我正在努力,所以投掷是不可能的。

【问题讨论】:

  • 随消息抛出异常不等于显示带有消息的消息框。你想让我做什么?如果要显示消息,请在 WinForms 中使用 MessageBox.Show("Test")。
  • 您是否尝试在“Catch”块中捕获异常。另外,请确保您没有在 Visual Studio 设置下选中“中断所有错误”。
  • 如果你想用自定义文本创建你自己的异常,你需要实现你自己的从Exception派生的自定义异常类。例如。 blogs.msdn.microsoft.com/agileer/2013/05/17/…

标签: c# try-catch throw unhandled-exception


【解决方案1】:

您需要catch 异常并提取要显示的消息。

try
{
     //Your code here
}
catch(Exception e)
{
     var message = e.Message;
     MessageBox.Show(message);
}

【讨论】:

    猜你喜欢
    • 2014-05-10
    • 2018-03-31
    • 2020-08-18
    • 1970-01-01
    • 1970-01-01
    • 2015-06-04
    • 2021-02-08
    • 1970-01-01
    • 2013-05-24
    相关资源
    最近更新 更多