如何忽略异常并完成尝试 [英] How to ignore an exception AND complete the try

查看:199
本文介绍了如何忽略异常并完成尝试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我一直在与此问题进行斗争一周以来,我认为我已经知道了这个问题,但是在确定下来之前,我不想在那儿报告答案.

简而言之,尝试使用SerialPort.Open()命令时出现IOException.事实证明,这是非常普遍的,大多数终端程序实际上也是如此,但是他们只是忽略了它.请阅读我上面的文章以获取完整的故事.现在我想做的就是忽略IOException,但是仍然打开串行端口.我不能通过try/catch来做到这一点,或者至少我不知道该怎么做.

我想知道是否可以尝试一些方法,并以某种方式声明我知道会抛出一个问题,这是一个安全的问题,我选择忽略异常并继续执行任务".需要明确的是,我不想忽略该错误然后继续前进.我想忽略该错误并仍然完成操作.

以下是我的最佳猜测,但这是行不通的.

        try
        {
            serialPort1.Open();               
        }
        catch (System.IO.IOException)
        {
            MessageBox.Show("An IO Exception occurred.");
        }
        finally
        {          
            //SAFELY IGNORE ERROR AND DO TASK ANYWAY HERE
        }

如果有人可以帮助我,我将非常感激.

如果之后再添加代码serialport1.Open,我将得到:

如果没有try/catch,这基本上是相同的事情.我想做的是说我正在尝试执行此操作:我不在乎它会引发错误,还是要执行此操作.

解决方案

通常,如果您有一行代码导致异常,并且无论是否发生异常,都需要做其他工作,您应该只使用catch,然后将要完成的工作放在try/catch之外(之后).


对于您的特殊情况(在另一个问题中描述),您正在处理的是.NET System.IO.Ports.SerialPort的可怕损坏.打开串行端口成功,但是SerialPort.Open是一种复杂的方法,它会执行大量额外的操作,并且如果这些额外(和不必要的)操作中的任何一个失败,则坚持要关闭端口.我还发现了System.IO.Ports.SerialPort的其他问题(例如,无法通过其内部设备名称打开端口). IMO,越早摆脱System.IO.Ports.SerialPort越好.

您可以p/调用Win32串行端口功能(CreateFile,其余为此处),或者找到已经编写了包装器库的人(我这样做了,但是它不公开).

So I've been battling this issue for about a week now and think I know the issue but I don't want to report an answer there until I have it pegged down.

In a nutshell, I get an IOException when trying to use the SerialPort.Open() command. As it turns out, this is very common and most terminal programs actually do as well, but they simply ignore it. Please read my post above for the full tale. Now what I want to do is ignore the IOException but still open the serial port. I cannot do this with try/catch, or at least I don't know how.

I was wondering is there a way to try something and somehow state that "I know an issue will be thrown, it is a safe problem and I am choosing to ignore the exception and carry on with the task". To be clear, I don't want to ignore the error then move on. I want to ignore the error and still complete the operation.

Below is my best guess, but it doesn't work.

        try
        {
            serialPort1.Open();               
        }
        catch (System.IO.IOException)
        {
            MessageBox.Show("An IO Exception occurred.");
        }
        finally
        {          
            //SAFELY IGNORE ERROR AND DO TASK ANYWAY HERE
        }

If anyone could help me out with this I'd be most appreciative.

EDIT: If I just add the code serialport1.Open afterwards I get:

This is basically the same thing that would happen without the try/catch. What I want to do is say I'm trying to do this: I don't care that it throws an error, do it anyway.

解决方案

In general, if you have a line of code causing an exception, and there's additional work to be done whether or not the exception occurred, you should just use a catch, and put the stuff you want done anyway outside (after) the try/catch.


For your particular case (described in your other question), you are dealing with the horrible brokenness that is .NET System.IO.Ports.SerialPort. Opening the serial port succeeded, but SerialPort.Open is a complicated method that does a bunch of extra stuff, and insists on closing the port if any of that extra (and unnecessary) stuff fails. I've found other problems with System.IO.Ports.SerialPort as well (e.g. inability to open ports by their internal device name). IMO, the sooner you get rid of System.IO.Ports.SerialPort the better.

You can either p/invoke the Win32 Serial Port functions (CreateFile and the rest are here) or find someone who already wrote a wrapper library (I did, but it isn't available publically).

这篇关于如何忽略异常并完成尝试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆