简体   繁体   English

捕获另一个类抛出的异常?

[英]Catch exception thrown by another class?

I thought I could catch an exception thrown by another class in my project, but I must be doing it wrong. 我以为我可以在项目中捕捉到另一个类抛出的异常,但是我一定做错了。 In the first class, I'm surrounding my call to the other class with a try/catch block: 在第一堂课中,我用try / catch块围绕对另一堂课的调用:

try
{    
    ImportPowerPoint.CreateTitle(textBoxPpt.Text, textBoxPkg.Text);
}
catch (FormatException ex)
{
    MessageBox.Show(ex.Message, "ERROR",
    MessageBoxButtons.OK,
    MessageBoxIcon.Warning);
}

In the second class, this is where I'm throwing the exception: 在第二节课中,这是我引发异常的地方:

if (!_layoutMap[(int)Layouts.A].ContainsValue(Fields.Title))
    throw new FormatException("Standard (A) Layout does not contain a title.");

if (!_layoutMap[(int)Layouts.A].ContainsValue(Fields.Txt1))
    throw new FormatException("Standard (A) Layout does not contain a txt1.");

if (!_layoutMap[(int)Layouts.A].ContainsValue(Fields.Prompt))
  throw new FormatException("Standard (A) Layout does not contain a prompt.");

When I run the program, it breaks immediately where the exception is thrown, instead of displaying the error window that I defined in the try/catch block. 当我运行程序时,它会立即在引发异常的位置中断,而不是显示我在try / catch块中定义的错误窗口。 Am I not handling this try/catch correctly? 我不能正确处理尝试/捕获吗?

For clarification, I'm forcing the exception to occur by removing certain pieces from the PowerPoint that I'm parsing. 为了澄清起见,我通过从要解析的PowerPoint中删除某些片段来强制发生异常。 When the program fails, for instance, because I removed the Title Field, the exception thrown is of type FormatException. 例如,当程序失败时,由于删除了“ Title字段”,抛出的异常的类型为FormatException。 Shouldn't my catch in the calling class handle this? 如果不是我catch在调用类处理这个问题?

EDIT: I think I may have found out something that is causing this to happen. 编辑:我想我可能已经发现了导致这种情况发生的原因。 The exception being thrown is in another thread. 引发的异常在另一个线程中。 So, since it's in another thread, could this mean that this is why my try/catch isn't catching the exception? 因此,由于它在另一个线程中,这是否意味着这就是为什么我的try / catch没有捕获异常的原因?

It is breaking in the IDE to notify that the exception is thrown. 它在IDE中中断以通知已引发异常。 If you press F5 again to continue running, your catch handler should be hit. 如果再次按F5键继续运行,则应单击catch处理程序。

There is nothing class-related about exceptions. 异常与类没有任何关系。 Your calling code ( ImportPowerPoint.CreateTitle(...); ) should be able to catch exceptions thrown by CreateTitle() . 您的调用代码( ImportPowerPoint.CreateTitle(...); )应该能够捕获CreateTitle()引发的异常。

See if your Visual Studio is configured to catch all exceptions (opposed to catching only unhandled ones): 查看您的Visual Studio是否配置为捕获所有异常(与仅捕获未处理的异常相反):

  1. Go to the 'Debug' menu 转到“调试”菜单
  2. Select 'Exceptions...' 选择“例外...”
  3. Uncheck check boxes under the 'Thrown' column 取消选中“抛出”列下的复选框

You would typically want the IDE to catch unhandled exceptions, so I would keep the other column checked. 您通常希望IDE捕获未处理的异常,因此我将另一列保持选中状态。

The problem here appears to be because this is a multithreaded program. 这里的问题似乎是因为这是一个多线程程序。 I'm looking at using the AppDomain.UnhandledException Event to handle my exceptions. 我正在使用AppDomain.UnhandledException事件处理我的异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM