1

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)
2
  • 1
    Giving the stacktrace of the exception would be helpful.
    – Bozho
    Apr 23, 2010 at 17:41
  • Noona, you've been given a lot of great help below. Why not return the favor and upvote good answers and accept the best? Apr 23, 2010 at 20:29

6 Answers 6

4

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.

2
  • 1
    It's IllegalArgumentException, but this is the right answer.
    – erickson
    Apr 23, 2010 at 17:53
  • it's a NullPointerException in disguise :)
    – Bozho
    Apr 23, 2010 at 18:25
2

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..

2
  • Yes, as far as I know, when an exception is caught it really is caught. But runtime exceptions can fall through (they are unchecked exceptions). Apr 23, 2010 at 17:47
  • Like Marcus said, you might want to check that a runtime exception isn't getting thrown. You can test this by putting your method in a try catch. e.g. try{ bindProxySocket(proxyConn, hostName, params1); } catch (Exception e){ e.printStackTrace(); }
    – mpd
    Apr 23, 2010 at 17:59
0

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?

1
  • no, it shouldn't already exit at this point, I added the stack trace in the edit.
    – Noona
    Apr 23, 2010 at 17:51
0

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.

0
0

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():
}
1
  • it's also a good practice to clean the resources in the finally statement, but I was thinking that then the try block would be large if you're doing too many things in this piece of code until you reach the point where you want to clean resources.
    – Noona
    Apr 23, 2010 at 21:13
0

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.

1
  • Yes that's the general cause in most (?) situations, but it was a requirement in the homework since it's a server application, you don't want your server collapsing and stopping from serving other clients if it fails with one client only.
    – Noona
    Apr 23, 2010 at 21:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.