handling java exception

Posted by Noona on Stack Overflow See other posts from Stack Overflow or by Noona
Published on 2010-04-23T17:39:43Z Indexed on 2010/04/23 17:43 UTC
Read the original article Hit count: 393

Filed under:
|

This questions is related to java exception, why are there some cases that when an exception 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 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

© Stack Overflow or respective owner

Related posts about java

Related posts about exceptions