java program silent exit

H

Hongyu

I have a java program that makes silent exit sometime, i.e., it doesn't
print out any error information when it exit. The program was designed
to repeatedly read the content from a web server by changing a query
parameter. The URL reader code is based on the simple java.net.URL
class, and looks like

//open reader for URL
InputStream is = new URL(urlString).openStream();
BufferedReader in = new BufferedReader(new InputStreamReader(is));

// read contents into a string buffer
while((int ch = in.read()) != -1) {
buffer.append((char)ch);
}
is.close();

By repeatedly calling the above URL reader, my java program can
continuously read tens of thousands of web pages, but then it somehow
can suddenly just stop working and exit. Based on tracing print lines,
I know the stop point is right at the "BufferedReader in = ..." line
most times, and ocasionally at the "buffer.append ..." line. But I
don't understand why URL class wouldn't throw any exception information
that user could catch, instead of a silent exit.

I would appreciate any enlightenment.
 
J

Jim

I have a java program that makes silent exit sometime, i.e., it doesn't
print out any error information when it exit. The program was designed
to repeatedly read the content from a web server by changing a query
parameter. The URL reader code is based on the simple java.net.URL
class, and looks like

//open reader for URL
InputStream is = new URL(urlString).openStream();
BufferedReader in = new BufferedReader(new InputStreamReader(is));

// read contents into a string buffer
while((int ch = in.read()) != -1) {
buffer.append((char)ch);
}
is.close();

By repeatedly calling the above URL reader, my java program can
continuously read tens of thousands of web pages, but then it somehow
can suddenly just stop working and exit. Based on tracing print lines,
I know the stop point is right at the "BufferedReader in = ..." line
most times, and ocasionally at the "buffer.append ..." line. But I
don't understand why URL class wouldn't throw any exception information
that user could catch, instead of a silent exit.

I would appreciate any enlightenment.
Maybe......

InputStream is = new URL(urlString).openStream();

If your urlString should be null, or malformed you will not contruct
a new URL object which will then cause the openStream() method to
fail with NullPointerException. JavaDoc says the URL constructor
may throw a MalformedURLException.

Try putting this all in a try..catch block and see what happens.

Jim
 
H

Hongyu

Thanks for response.

I do have all the exceptions taken care of, including
NullPointerException and InterruptedException
 
M

Michiel Konstapel

Thanks for response.

I do have all the exceptions taken care of, including
NullPointerException and InterruptedException

Try wrapping the whole block in a try { ... } catch (Throwable t)
{ t.printStackTrace() } block. Then, nothing thrown from Java will get
through.
Michiel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top