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. Is this possible to detect it from Socket Exception object ?
3 Answers
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.

- 46,613
- 43
- 151
- 237
-
For information about exception chaining see http://stackoverflow.com/questions/5020876/chained-exception-in-java – Raedwald Jun 27 '14 at 07:47
You can't get it from the excpetion, because ther is no property where the socket is saved.

- 67,715
- 15
- 98
- 113
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. You can see in this exception class or any of it's supertypes. None of them is able to hold reference to any object.

- 2,029
- 14
- 22
-
Yes, I have searched for custom user objects. But Java doesn't support that unlike .Net has dictionary and it stores some relevant objects there. – Rajdip Patel Jun 27 '14 at 06:59
-
Then how to detect which socket fired SocketException ? Will I have only one option to detect it from socket object's state ? Is there only one way ? – Rajdip Patel Jun 27 '14 at 06:59
-
Can you add some of your code here so we can analyze it first? Also, some stacktrace would help. – Aakash Jun 27 '14 at 07:56
-
*None* of them? There are no exceptions anywhere to the principle you have stated here? – user207421 Jun 27 '14 at 08:59
-
-
Come off it. I expressed myself clearly. Have you really done an exhaustive search of all exceptions in the JDK to determine that none of them contains a source property? – user207421 Jun 27 '14 at 09:46
-
@EJP, Well, the context of that line is very well defined by it's previous line. You can read the post again. – Aakash Jun 27 '14 at 10:13
-
That's just meaningless blather. I didn't ask about 'the context of that line'. You haven't actually answered my question, asked three times now. – user207421 Jun 27 '14 at 12:19
-
1. Every question and answer has a context. Without a context, there could be a use-less answer to the question. E.g. OP has asked about the SocketException, which very well defines the context. 2. I am not going to check all the classes in JDK to look for the suitable property. Again, see point 1. 3. My answer answers the problem OP is facing. If you have any query, you can post it in different question. 4. I am not going to post any more comment in use-less discussion and waste my precious time. And by the way, if you can't understand the post, you should not mark the post as "not useful". – Aakash Jul 03 '14 at 06:27