简体   繁体   English

套接字异常-如何从套接字异常对象获取原始套接字

[英]Socket Exception - How to get original socket from Socket Exception object

I have one method that transfers data from one socket to another. 我有一种方法可以将数据从一个套接字传输到另一个套接字。 Now whenever there is some problem either on any socket I need to detect which socket has caused Socket Exception. 现在,无论何时在任何套接字上出现问题,我都需要检测哪个套接字导致了Socket Exception。 Is this possible to detect it from Socket Exception object ? 是否可以从Socket Exception对象检测到它?

Not from the exception itself, but you can from the context. 不是来自异常本身,而是来自上下文。 If the code manipulates only one socket, clearly the exception must relate to that socket. 如果代码仅操纵一个套接字,则显然该例外必须与该套接字有关。

It sounds like you are not handling your exceptions at an appropriate level, instead handling (catching) a low level exception at a high level part of your program, and expecting to be able to do low level handling at that point. 听起来您好像没有在适当的级别上处理异常,而是在程序的高级部分处理(捕获)低级别的异常,并希望能够在那时进行低级别的处理。

Consider catching the exception at an intermediate level, partially handling it, then rethrowing it. 考虑在中间级别捕获异常,对异常进行部分处理,然后将其重新抛出。 You might want to do exception translation (chaining) at that point. 您可能需要在此时进行异常转换(链接)。

您无法从专有程序中获取它,因为这不是保存套接字的属性。

Simple answer is 'NO'. 简单的答案是“否”。 Since Exceptions in Java are made to notify an unexpected behavior, they are not made to store any reference to the object in error. 由于Java中的异常是用来通知意外行为的,因此不会将它们存储为错误对象的任何引用。 You can see in this exception class or any of it's supertypes. 您可以在此异常类或其任何超类型中看到。 None of them is able to hold reference to any object. 它们都不能保存对任何对象的引用。

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

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