c# - 为什么我的错误没有被捕获?

标签 c# error-handling

我有一个包含 Phone 对象的类。

电话是虚拟电话,在我的类的构造函数中,我传入了我希望连接的虚拟电话的 IP 地址和端口。

在构造函数内,我运行一个名为 Connect 的方法。在 Connect 方法中,我从电话的 IP 地址/端口组合中实例化一个 IPEndPoint 对象,然后实例化一个 Socket 对象,并且运行 Socket 对象的 Connect 方法,传入我的 IPEndPoint 作为参数。

如果未针对特定电话(不是我的工作)正确配置电话服务器,连接将被拒绝,并引发SocketException。我正在 try catch 这个异常。

这是一个 Windows 窗体应用程序。在 Form 对象的范围内,但在任何构造函数/方法的范围之外,我将我的手机作为未实例化的私有(private)字段,如下所示(iPhone 是我的手机使用的接口(interface)):

private IPhone _phone;

我有一个名为 SetupPhone 的方法,我在此处实例化我的电话对象,并尝试在此处捕获异常:

private void SetupPhone()
{
    try
    {
        _phone = new Phone(AgentDetails.IPAddress, AgentDetails.Port);
    }
    catch(SocketException ex)
    {
        Log.LogException("Error mapping phone to port", ex);
        ShowBaloonTip("An error occured starting CTI. Please select your name from the list to try again", ToolTipIcon.Error);

        ChangeUser();
        return;
    }

    //Subscribe to Phone events here
}

这是电话对象的构造函数:

public Phone(string ipAddress, int port, string password = "FooBar")
{
    Connect(ipAddress, port, password);
}

这是连接方法:

public void Connect(string ipAddress, int port, string password)
{
    _phone = new IPEndPoint(IPAddress.Parse(ipAddress), port);
    _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);

    _socket.Connect(_phone);

    //Redacted
}

在未正确配置的手机上,我的 SocketException 抛出如下所示:

Exception

我认为在对象构造函数中运行的任何方法抛出的任何异常都应该由放置在该对象实例化周围的 try/catch block 安全地捕获,这是不是正确的?或者事实并非如此?我想捕获实例化对象时可能发生的任何错误,这不可能吗?

最佳答案

http://www.blackwasp.co.uk/VSBreakOnException.aspx

When running a program in debug mode, using Visual Studio's debugger, and encountering an exception, the default behaviour is to pause execution unless the error is handled in a try / catch block. This behaviour can be modified for each exception type.

Debug模式异常

When you execute your software in debug mode, Visual Studio reacts differently to a thrown exception depending upon whether the exception is handled within a try / catch / finally block or is unhandled. When using the default configuration, unhandled exceptions cause the program to halt and the exception details to be displayed. Handled exceptions do not cause the program to stop. You can see this by executing the following code in debug mode. Although a DivideByZeroException is thrown when the division is attempted, the program continues. If you comment out the try and catch, the exception halts execution.

try
{
    int i = 0;
    int j = 1;
    Console.WriteLine(j / i);
}
catch
{
    Console.WriteLine("Caught an exception");
}
Console.ReadLine();

This behaviour is useful in most circumstances. However, sometimes you will want to ignore a specific type of exception, even when unhandled, or break on a handled exception that would normally be ignored. You can control this using the Exceptions dialog box, which may be viewed by selecting "Exceptions" from the Debug menu or by pressing Ctrl-Alt-E.

The main area of the dialog box shows a list of exception types in a tree structure. The branches of the tree can be expanded to show various groups of exceptions and the individual types within each category. Two checkboxes are shown for each exception and group. If the "Thrown" checkbox is ticked, the program will break when the selected exception, or one of the selected group of exceptions, is encountered. This includes exceptions that have been handled. If the "User-unhandled" checkbox is ticked the program will break only if the exception is unhandled. To try the options, find the DivideByZeroException type in the tree structure. To find an exception quickly, click the Find button and enter part of the name of the item you are searching for. The first matching item will be found. If this is not the desired exception, click the Find Next button to cycle through the matches. Once you have found DivideByZeroException, check the appropriate "Thrown" option. If you run the sample program you will see that the program halts on the handled exception.

全部重置

There are some further options in the Exceptions dialog box that are worthy of note. The first of these is the Reset All button. If you have changed the options in the dialog box to help debug your application, you can reset all of the options to their original settings by clicking this button.

配置自定义异常(exception)

If you have defined your own exception type that inherits functionality from the Exception class or one of its subclasses, you may wish to configure it using the dialog box. As it will not be a standard exception type, it will not appear in the list by default. However, you can add it by clicking the Add button and providing the details. For .NET exceptions you should ensure that you choose the "Common Language Runtime Exceptions" option from the drop-down list. You should then provide the fully qualified name of the exception class. For example, "MyNamespace.MyException".

关于c# - 为什么我的错误没有被捕获?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13330758/

相关文章:

web - 如何将域名从one.com链接到digitalOcean.com?

c# - 在 C# 5 中多次异步调用同一个方法是否安全

c# - 名称 metrowindow 不存在?

c# - 如果所有内容都应该用引号引起来,如何处理绑定(bind)方法

c# - 这里发生了什么?没有默认构造函数时如何调用?

ruby-on-rails - 我的应用无法从服务器加载一些图像

php - PHP错误.htaccess限制文件夹深度

Python http 错误日志记录

c# - 底层提供程序在 ConnectionString PostgreSql EF6 上失败

java - 如何查看 java REST 服务中未处理异常的原因?