• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3152
  • Last Modified:

Exception when using the xmlreader.

I am using the XmlReader to Read a url.
<snip>
XmlReader reader = null;
reader = new XmlReader(url);
</snip>.

some times I am getting the exception:

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 502 for URL: xxxx.com
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1436)
        at com.sun.syndication.io.XmlReader.<init>(XmlReader.java:174)
        at com.sun.syndication.io.XmlReader.<init>(XmlReader.java:151)
        at sameple.main(romeexample5.java:35)
=

Can you pl let me know how I can avoid exceptions like this?
As per rfc 502 is Bad Gateway
0
vkchaitu82
Asked:
vkchaitu82
  • 9
  • 7
  • 5
2 Solutions
 
for_yanCommented:
Looks like somethig wrong on their (server) end

http://www.checkupdown.com/status/E502.html
0
 
objectsCommented:
that suggests an issue with the server, not with your code

http://www.checkupdown.com/status/E502.html
0
 
objectsCommented:
theres nothing you can do in your code to handle a 502

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
vkchaitu82Author Commented:
I agree that it is a server issue :-)
But my code should not exit with exception.


I was looking how I can handle exception like that?
I have this xmlreader is a loop and once this issue comes it exits
0
 
vkchaitu82Author Commented:
some times I get this exception.


Exception in thread "main" java.net.ConnectException: Connection timed out: connect
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
        at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
        at java.net.Socket.connect(Socket.java:529)
        at java.net.Socket.connect(Socket.java:478)
        at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
        at sun.net.www.http.HttpClient.<init>(HttpClient.java:233)
        at sun.net.www.http.HttpClient.New(HttpClient.java:306)
        at sun.net.www.http.HttpClient.New(HttpClient.java:323)
        at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:970)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:911)
        at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:836)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1172)
        at com.sun.syndication.io.XmlReader.<init>(XmlReader.java:174)
        at com.sun.syndication.io.XmlReader.<init>(XmlReader.java:151)


This is because of time out there is no 4xx/5xx response at all from server . I am looking for a way to get out of this exceptions so that my loop works
0
 
objectsCommented:
catch the IOException, log it and continue
0
 
for_yanCommented:


Sure you can catch it
write notification abiut the erroe and
continue:

try {

//thi where you line 174 should be

}catch(Exception ex){

System.out.println(ex.ToString());
ex.printStackTrace();
}

0
 
objectsCommented:
try {
   // do your stuff here
} catch (ConnectException ex) {
   // log error

} catch (IOException ex) {
   // log error
}
0
 
for_yanCommented:
This is all in the same way - catch them, log or print some info
ad it will go forward unless you put System.exit(0) in the catch loop
0
 
objectsCommented:
and put the try catch block I posted above *inside* your loop
while (whatever) {
   try {
   // do your stuff here
   } catch (ConnectException ex) {
   // log error

} catch (IOException ex) {
   // log error
}

}
0
 
vkchaitu82Author Commented:
I am stil getting some more exceptions like:
Exception in thread "main" com.sun.syndication.io.ParsingFeedException: Invalid XML
        at com.sun.syndication.io.WireFeedInput.build(WireFeedInput.java:185)
        at com.sun.syndication.io.SyndFeedInput.build(SyndFeedInput.java:122)
        at romeexample5.main(romeexample5.java:245)
Caused by: java.io.IOException: Stream closed
        at sun.nio.cs.StreamDecoder.ensureOpen(StreamDecoder.java:27)
        at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:128)
        at java.io.InputStreamReader.read(InputStreamReader.java:167)
        at com.sun.syndication.io.XmlReader.read(XmlReader.java:297)
        at java.io.Reader.read(Reader.java:104)
        at com.sun.syndication.io.impl.XmlFixerReader.trimStream(XmlFixerReader.java:67)
        at com.sun.syndication.io.impl.XmlFixerReader.read(XmlFixerReader.java:221)
        at com.sun.syndication.io.impl.XmlFixerReader.read(XmlFixerReader.java:302)
        at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1742)
        at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.arrangeCapacity(XMLEntityScanner.java:1619)
        at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.skipString(XMLEntityScanner.java:1657)
        at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:193)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:772)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
        at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
        at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
        at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
        at org.jdom.input.SAXBuilder.build(SAXBuilder.java:518)
        at org.jdom.input.SAXBuilder.build(SAXBuilder.java:946)
        at com.sun.syndication.io.WireFeedInput.build(WireFeedInput.java:178)


Is there a way I can catch all the exceptions?
0
 
objectsCommented:
yes, just add another catch block
and include a catch all


while (whatever) {
   try {
   // do your stuff here
   } catch (ConnectException ex) {
   // log error

} catch (IOException ex) {
   // log error
} catch (ParsingFeedException ex) {
   // log error
} catch (Exception ex) {
   // log error
}

}
0
 
vkchaitu82Author Commented:
I am getting:

 cannot find symbol
symbol  : class ParsingFeedException
location: class sample.
                        catch (ParsingFeedException ex)
                               ^
1 error

==
I have  import java.io.IOException;
should I add any other package
0
 
objectsCommented:
import com.sun.syndication.io.ParsingFeedException;
0
 
for_yanCommented:

I'd rathe catch general Exception, just like that:

catch(Exception ex) {

}

and the you print .toString()  and printStackTrace()- it will tell you
what rthis exception is about.
I know it is recommended to cascade it this way,
but it just takes additional hassle, and gives not much
0
 
vkchaitu82Author Commented:
exception com.sun.syndication.io.ParsingFeedException is never thrown in body of corresponding try statement
                        catch (ParsingFeedException ex)
                        ^
1 error
==
2) if I add   catch (ConnectException ex)

I am getting this exception
 cannot find symbol
symbol  : class ConnectException
location: class sample
                          catch (ConnectException ex)
                                 ^

==
try {
                                URL  url = new URL;
                              reader = new XmlReader(url);
                    }
                    catch (IOException ex)
                    {
                         // System.out.println(ex.ToString());

                    }
                    catch (ConnectException ex)
                    {
                     // log error
                    }                     
                    catch (ParsingFeedException ex)
                    {
                     // log error
                    }                     
                    catch (Exception ex)
                    {
                     // log error
                    }
0
 
for_yanCommented:
I guess, imprt

java.net.ConnectException - ?

just catch Exception ... and no problems
0
 
objectsCommented:
import java.net.ConnectException;

> exception com.sun.syndication.io.ParsingFeedException is never thrown in body of corresponding try statement

sounds like that exception is being thrown in a different block of you rcode
0
 
vkchaitu82Author Commented:
exception java.net.ConnectException has already been caught
                          catch (ConnectException ex)
                          ^
 exception com.sun.syndication.io.ParsingFeedException is never thrown in body of corresponding try statement
                          catch (ParsingFeedException ex)
                          ^
2 errors

==
no it same code and same line I mentioned
0
 
objectsCommented:
> exception java.net.ConnectException has already been caught

you need to catch the ConnectException *before* the IOException
see the code I posted earlier

>  exception com.sun.syndication.io.ParsingFeedException is never thrown in body of corresponding try statement

That exception is being throw further down in the code (line 245)
move the try catch down there

Exception in thread "main" com.sun.syndication.io.ParsingFeedException: Invalid XML
        at com.sun.syndication.io.WireFeedInput.build(WireFeedInput.java:185)
        at com.sun.syndication.io.SyndFeedInput.build(SyndFeedInput.java:122)
        at romeexample5.main(romeexample5.java:245)
0
 
vkchaitu82Author Commented:
Thanks Objects one exception is gone.


>  exception com.sun.syndication.io.ParsingFeedException is never thrown in body of corresponding try statement
 is still there
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 9
  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now