如果socketaccept导致异常,如何找出原因?

发布于 2024-12-21 19:28:28 字数 705 浏览 0 评论 0原文

我正在创建一个函数,它将执行套接字接受并返回 3 个值 0=退出线程发生非常严重的错误 1=确定与连接对话 =发生了什么事,再做一次接受(超时)。

我看到 IOException 有一个 GetCause 方法,它返回一个可抛出对象。 这个 throwable 对象有一个返回 throwable 的 get Cause 方法,该方法有一个返回 throwable 的 getcuase 方法,似乎这种情况会永远持续下去,继续获取另一个 throwable 对象。

我怎样才能得到异常蚂蚁关闭的原因??? 我可以使用 get Reason 和一堆字符串比较,但这似乎并不可靠。 特德

int GetClient()
{
    try {
        server.setSoTimeout(5*1);  
        connection=server.accept();
    }
    catch(IOException ec)
    {
        System.out.println(Thread.currentThread()+":"+ec.getMessage()); 


        return 2; // for time out or something where we can try again
        // return a zero saying we must stop erra o bad

    }   
    return 1;
}

I'm creating a function that will do a socket accept and return 3 vales
0=really bad error happen exit thread
1=ok talk to the connection
=something happen, do another accept (time out).

I see the IOException has a GetCause meths that returns a throwable object.
this throwable object has a get cause method that returns a throwable, which has a getcuase method returning a throwable, seems like this would go on forever, keep getting another throwable object.

How can I get the reason the exception ant off?????
I could use get reason and a bunch of string compares, but this does not seem to reliable.
Ted

int GetClient()
{
    try {
        server.setSoTimeout(5*1);  
        connection=server.accept();
    }
    catch(IOException ec)
    {
        System.out.println(Thread.currentThread()+":"+ec.getMessage()); 


        return 2; // for time out or something where we can try again
        // return a zero saying we must stop erra o bad

    }   
    return 1;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑脸一如从前 2024-12-28 19:28:28
accept()

抛出各种异常。
SocketTimeoutException 扩展了 IOException,但您可以在捕获一般 IOException 之前“捕获”它。这将允许您返回建议您可以重试的值。

这是否涵盖了您的 3 种情况,Ok、IOException 和 Timeout?

accept()

throws a variety of exceptions.
SocketTimeoutException extends IOException but you can 'catch' it before catching the general IOException. This will allow you to return the value suggesting you can try again.

Does this cover your 3 cases, Ok, IOException and Timeout?

天煞孤星 2024-12-28 19:28:28

如果您只对超时异常感兴趣,那么单独捕获这些异常。大多数 IO 方法都可能抛出 IOException,但 IOException 有许多不同的子类(它们本身还有更多子类),您可以单独捕获和处理。

例如。

try {
    conn = server.accept();
} catch (SocketTimeoutException e) {
    return 2;
} catch (IOException e) {
    // socket exception will not be recaught
    // even if return statement wasn't used
    return 0;
}
return 1;

提供了 getCause 方法,因为当创建异常时,可以使用字符串消息来创建它们,也可以使用可能导致抛出此异常的异常来创建。从而允许异常捕获者看到导致异常的完整细节。

例如。

public double addNumbers(String a, String b) {

    try {
        double i = Double.parseDouble(a);
        double j = Double.parseDouble(b);
        return i + j;
    } catch (NullPointerException cause) {
        throw new IllegalArgumentException(
            "Arguments aren't allowed to be null", cause);
    } catch (NumberFormatException cause) {
        throw new IllegalArgumentException(
            "One or more arguments weren't numbers", cause);
    }
}

上面的例子有点迟钝,但它演示了一个原则:可能会捕获某些异常(也许它可能会尝试从中恢复),然后抛出一个新的异常,并以原始异常为原因。使用 getCause 方法可以让调用者立即看到 IllegalArgumentException 最初是在 addNumbers 方法中引发的(这就是用户代码库中的问题所在)。但是,通过查看原因,他们将能够看到有关参数非法的更详细消息(NumberFormatException 包括尝试解析的字符串)。

If you're only interested in timeout exceptions then catch those exceptions separately. Most IO methods can potentially throw an IOException, but there are many different subclasses of IOException (which themselves have further subclasses) that you can catch and deal with separately.

eg.

try {
    conn = server.accept();
} catch (SocketTimeoutException e) {
    return 2;
} catch (IOException e) {
    // socket exception will not be recaught
    // even if return statement wasn't used
    return 0;
}
return 1;

The getCause method is provided as when exceptions are created they can be created with string message, but also the exception that may have caused this exception to be thrown. Thus allowing catchers of the exception to see the full detail of what caused the exception.

eg.

public double addNumbers(String a, String b) {

    try {
        double i = Double.parseDouble(a);
        double j = Double.parseDouble(b);
        return i + j;
    } catch (NullPointerException cause) {
        throw new IllegalArgumentException(
            "Arguments aren't allowed to be null", cause);
    } catch (NumberFormatException cause) {
        throw new IllegalArgumentException(
            "One or more arguments weren't numbers", cause);
    }
}

The above case is a little obtuse, but it demonstrates the principal that some exception may be caught (that maybe it might try to be recovered from) and then a new exception thrown with the original exception as the cause. Having a getCause method allows the caller to immediately see that the IllegalArgumentException was originally thrown in the addNumbers method (and that this is where the problem in the user code base is). However, by looking at the cause they will be able to see a more detailed message about by the argument was illegal (NumberFormatException includes the string that was trying to be parsed).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文