• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Not able to Catch java.net.SocketTimoutException

 
Mohan Vinukonda
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I've a scenario to catch java.net.SocketTimeoutException and send some email, I am getting the error msg as "Unreachable catch block for SocketTimeoutException. This exception is never thrown from the try statement body".

Can you please let me know how to handle SocketTimeoutException. Thanks in advance.


-Mohan
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code doesn't throw that type of Exception.

Catch the generic exception and process accordingly.

WP
 
Mohan Vinukonda
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.

I've a scenario to handle SocketTimeoutException, I need to send some email if I get this exception. How this possible in generic exception.

-Mohan
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohan Vinukonda wrote: I need to send some email if I get this exception.

The whole point of the error message is that you will never get that exception thrown, so there is no point in writing code to handle it.
Why don't you show us the code and tell us where you think the exception will be thrown from.
 
Mohan Vinukonda
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is the back ground.

We have a web service client which calls a service. I am setting a timeout parameter for the web service call. There are possibilities that the requests can get timeout due to network issues, which intern creating a hung threads on the server. I am trying to handle this situation by setting a timeout parameter on the WebService call. Now, for testing the scenario I've narrow down response time and testing it. Here I could simulate the issue. I am not able to handle the sockettimeoutexception in here.


[mg]added code tags (UseCodeTags)
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote: and tell us where you think the exception will be thrown from.

Which method call in your code do you think is going to throw a SocketTimeoutException ?
 
Mohan Vinukonda
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
strFormatedString = (String) call.invoke( new Object[] {appID,requestXML} ); will throw SocketTimeoutException, Since the WebService all will require a little more time to get the response.

thank
Mohan
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If "Call" is actually javax.xml.rpc.Call or org.apache.axis.client.Call, then no it doesn't. The first can throw an java.rmi.RemoteException, javax.xml.rpc.soap.SOAPFaultException or javax.xml.rpc.JAXRPCException, the second can only throw an java.rmi.RemoteException. No SocketTimeoutException there.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohan Vinukonda wrote:strFormatedString = (String) call.invoke( new Object[] {appID,requestXML} ); will throw SocketTimeoutException, Since the WebService all will require a little more time to get the response.

thank
Mohan


Can you provide a link to the javadocs for invoke() that show that exception in the method's throws clause?
 
Mohan Vinukonda
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
currently I am catching the exception in generic Exception catch block, there I am printing the stack trace(fillInStackTrace). It is printing as java.net.SocketTimeoutException. I'll try to catch RemoteException and post the response now.

Best
Mohan
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohan Vinukonda wrote:currently I am catching the exception in generic Exception catch block, there I am printing the stack trace(fillInStackTrace).

That doesn't actually print a stack trace. I see your code for this is:

You're ultimately just using String concatenation on the se exception, which just invokes toString() and gives you the class name and message of the exception. To see a proper stack trace, the easiest way is to use your logger's methods for displaying an exception. Try:

This will give you (and us) a lot more information about what is really happening.
 
Mohan Vinukonda
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried your suggestions now, It is actually throwing RemoteException however when I print stacktrace it is giving msg as "java.net.SocketTimeoutException: Read timed out" even it is printing the same msg for fillInStackTrace

Best,
Mohan
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method fillInStackTrace should be ignored. It's poorly documented, but it does not actually print a stack trace, or convert it to a string. It's called internally by other code; you don't need to worry about it. In particular you should never try to print its result and expect it to tell you anything useful. It just returns the original exception. If you print it, you just call toString() on it. AND YOU DON'T GET A STACK TRACE FROM IT. Using that method does NOT "print stacktrace". Ever.
 
Mohan Vinukonda
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mike, what about the message printed by StackTrace. Do yo think msg given by stack trace because of SocketTimeoutException. Ultimately RemoteException happened because of SocketTimeoutException?
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it's ultimately caused by a SocketTimeoutException.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50278
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But even if the RemoteException is caused by a SocketTimeoutException, you cannot catch it with catch(SocketTimeoutException ste)

Moving thread as too difficult for this forum.
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You indeed cannot catch it as a SocketTimeoutException. What is possible is to catch the RemoteException, retrieve it's cause and check if that's an instanceof SocketTimeoutException.
 
Mohan Vinukonda
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that is wonderful Rob, good way to handle actual SocketTimeoutException. I'll try to handle the way you suggested.
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic