0

I have a simple program here:

public class Main {
    private static Connection connectON = null; 
    private static PreparedStatement preparedStatementON = null;

    public static void main (String args[]) throws Exception {        
    try{ 
        Class.forName("org.mariadb.jdbc.Driver");
        connectON = DriverManager.getConnection("jdbc:mysql:/234234/ /?"+"user t& d= 3"); 
        System.out.println("Trying to connect to online"+connectON);                 
        System.out.println( "-----MAIN----");
        BaseDataUploader da = new BaseDataUploader();da.readDataB ();
    }
    catch (Exception e) {
        BaseDataUploader da2  = new BaseDataUploader();  //loads data from DFA to base 
        da2.errorLog(e,0000);
        throw e;
    }
    finally { 
        if(preparedStatementON !=null)
            preparedStatementON.close();
        if(connectON !=null)
            connectON.close();
        }  
    }
}

Now I want to know if new objects are created inside the catch statement even if an exception has not occured? Thanks guys.

Kara
  • 6,115
  • 16
  • 50
  • 57
Enrico
  • 77
  • 2
  • 4
  • 19

2 Answers2

1

The code in catch block is executed only if the Exception has occurred - so if you create an object there it will be created only if the exception is caught. The code in final block executes always. Btw, much faster than posting question here would be to try something like this yourself:

        Object o = null;
        boolean throwIt = false; //or true
        try{

            if (throwIt)
                throw new Exception();
        }catch (Exception e){
            o = new Object();
        }

        System.out.println(o);
ps-aux
  • 11,627
  • 25
  • 81
  • 128
  • Now I'm pretty sure that objects inside my catch statements aren't causing the consumption of too many heap memory.. – Enrico Aug 07 '13 at 07:43
0

You need to understand how try-catch-finally block works to answer this question. If you read about these blocks, you yourself could answer this question. But to help you in this, if no exception occurs catch block will not be executed and hence the object will not be created.
The object is created within the catch block, so if this block does not get executed, objects wont be created as well.

This link explains the basic working of try-catch-finally block.

Ankur Shanbhag
  • 7,746
  • 2
  • 28
  • 38
  • yeah, I just wondered why this simple program consumes too many heap memory.. In that case, I might just be missing some freeing up of references here.. – Enrico Aug 07 '13 at 07:41
  • In java, you need not worry about freeing the memory. GarbageCollector takes care of this. you just need to make objects go out of reference to get them garbage collected – Ankur Shanbhag Aug 07 '13 at 07:46
  • exactly, so how can i get those references out? (complete newbie) – Enrico Aug 07 '13 at 07:46
  • There are several ways to achieve this. I cannot explain it over the comment. Please refer this link for better understanding : http://pawlan.com/monica/articles/refobjs/ – Ankur Shanbhag Aug 07 '13 at 07:50