aspose file tools*
The moose likes Java in General and the fly likes Not able to Catch java.net.SocketTimoutException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Not able to Catch java.net.SocketTimoutException" Watch "Not able to Catch java.net.SocketTimoutException" New topic
Author

Not able to Catch java.net.SocketTimoutException

Mohan Vinukonda
Ranch Hand

Joined: Jul 28, 2005
Posts: 32
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

Joined: Mar 28, 2012
Posts: 859

Your code doesn't throw that type of Exception.

Catch the generic exception and process accordingly.

WP
Mohan Vinukonda
Ranch Hand

Joined: Jul 28, 2005
Posts: 32
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

Joined: Aug 05, 2005
Posts: 3557
    
  15
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.


Joanne
Mohan Vinukonda
Ranch Hand

Joined: Jul 28, 2005
Posts: 32
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

Joined: Aug 05, 2005
Posts: 3557
    
  15
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

Joined: Jul 28, 2005
Posts: 32
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

Joined: Oct 27, 2005
Posts: 19696
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jul 28, 2005
Posts: 32
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

Joined: Mar 05, 2008
Posts: 3014
    
  10
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

Joined: Jul 28, 2005
Posts: 32
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

Joined: Mar 05, 2008
Posts: 3014
    
  10
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

Joined: Jul 28, 2005
Posts: 32
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

Joined: Mar 05, 2008
Posts: 3014
    
  10
Yes, it's ultimately caused by a SocketTimeoutException.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
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

Joined: Oct 27, 2005
Posts: 19696
    
  20

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

Joined: Jul 28, 2005
Posts: 32
that is wonderful Rob, good way to handle actual SocketTimeoutException. I'll try to handle the way you suggested.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Not able to Catch java.net.SocketTimoutException