Reputation: 643
This questions is related to java exceptions, why are there some cases that when an exception is thrown the program exits even though the exception was caught and there was no exit() statement? my code looks something like this
void bindProxySocket(DefaultHttpClientConnection proxyConnection, String hostName, HttpParams params)
{
if (!proxyConnection.isOpen())
{
Socket socket = null;
try {
socket = new Socket(hostName, 80);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try
{
proxyConnection.bind(socket, params);
}
catch(IOException e)
{
System.err.println ("couldn't bind socket");
e.printStackTrace();
}
}
}
and then
I call this method like this:
bindProxySocket(proxyConn, hostName, params1);
but, the program exits, although I want to handle the exception by doing something else, can it be because I didn't enclose the method call within a try catch clause? what happens if I catch the exception again even though it's already in the method? and what should I do if i want to clean resources in the finally clause only if an exception occurs and otherwise I want to continue with the program? I am guessing in this case I have to include the whole piece of code until I can clean the resources with in a try statement or can I do it in the handle exception statement? some of these questions are on this specific case, but I would like to get a thorough answer to all my questions for future reference. thanks
edit:
java.net.UnknownHostException: www.dsewew324f.com
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at homework3.Proxy.bindProxySocket(Proxy.java:666)
at homework3.Proxy$3.handle(Proxy.java:220)
at org.apache.http.protocol.HttpService.doService(HttpService.java:293)
at org.apache.http.protocol.HttpService.handleRequest(HttpService.java:212)
at homework3.Proxy.start(Proxy.java:472)
at homework3.Proxy.main(Proxy.java:1282)
Exception in thread "main" java.lang.IllegalArgumentException: Socket may not be null
at org.apache.http.impl.DefaultHttpClientConnection.bind(DefaultHttpClientConnection.java:80)
at homework3.Proxy.bindProxySocket(Proxy.java:674)
Upvotes: 1
Views: 1120
Reputation: 1109735
You shouldn't continue the code after an exception which may cause that the code cannot continue.
Rewrite your code as follows:
Socket socket = null;
try {
socket = new Socket(hostName, 80);
try
{
proxyConnection.bind(socket, params);
}
catch(IOException e)
{
System.err.println ("couldn't bind socket");
e.printStackTrace();
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Better practice is just to let that exception go and declare a throws
in your method. You should only handle the exception where it makes sense to handle it. Don't suppress the exceptions by just printing the trace and then continuing with the program flow.
Upvotes: 0
Reputation: 2230
To answer some of your questions: what happens if I catch the exception again even though it's already in the method?
To put it simply, you can't. Once an exception is caught once, it is no longer on the top of the stack so any further attempts to catch it will fail
what should I do if i want to clean resources only if an exception occurs and otherwise I want to continue with the program?
If you want to do some action, any action, only when an exception occurs you should do this in your catch block.
I am guessing in this case I have to include the whole piece of code until I can clean the resources with in a try statement or can I do it in the handle exception statement?
I already answered this question with the one above :P
Like I said in my comment on marcus' post you should put a try catch around the call to the function itself to ensure that any other exceptions are being caught. You can figure out what to do from there when you know what exceptions aren't being caught.
try{
bindProxySocket(proxyConn, hostName, params1);
}
catch(Exception e){
e.printStackTrace():
}
Upvotes: 0
Reputation: 405
May be that proxyConn
is null, and because
if (!proxyConnection.isOpen())
it is not in a try/catch block it may generate an unhandled exception that causes your program to exit.
Upvotes: 0
Reputation: 48659
If
socket = new Socket(hostName, 80);
throws an exception then socket
will be null and
proxyConnection.bind(socket, params);
will throw a NullPointerException
, which you do not catch.
Upvotes: 4
Reputation: 56709
Are you sure the exception was caught? Your catch block only catches certain exceptions. A Runtime exception could be getting thrown which would not be caught..
Upvotes: 2
Reputation: 3129
Could it be that your program is simply coming to it's natural conclusion (exits from the main method)?
If your program is exiting because of an exception thrown from the main method then it should be printed to the console. Can you provide this stack trace?
Upvotes: 0