Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-09-2008, 05:39 PM   PM User | #1
BubikolRamios
Senior Coder

 
Join Date: Dec 2005
Location: Slovenia
Posts: 1,876
Thanks: 114
Thanked 76 Times in 76 Posts
BubikolRamios is on a distinguished road
try catch finaly and translate of delphi freeandnil ? (garbage collection)

want to clear some basic things. Coding in fashion that any of my functions called returns true or false(some kind of error happed)

1. does "finally {varname = null;" have any effect at all or should I trigger
garbage collector from code or what, and if so , how ?
2. the code below in case of err returns false, so finaly is not executed
but should be, how to code that nicely, of course i could invent new
variable and then if wariable = then return ... but this is ugly.
3. Logger.getLogger(classname .., where does this go, this is automaticaly
generated by ide, and have no idea where does it go ?
4. is there a way to ask in finaly part how I got there, by errror or by no error ?

Code:
 public  static boolean someFunc() 
 { 
   type varname = null;
   try
   {         
      type varname = new type();     
      // ....
   }
   catch (type ex)
   {
      Logger.getLogger(classname.class.getName()).log(Level.SEVERE, null, ex);
      return false;
   }
   finally 
   {         
     varname = null;
   }
   return true;
}
Thaks for replays.

Last edited by BubikolRamios; 03-09-2008 at 05:47 PM..
BubikolRamios is offline   Reply With Quote
Old 03-09-2008, 06:28 PM   PM User | #2
Aradon
Moderator-san


 
Aradon's Avatar
 
Join Date: Jun 2005
Location: USA
Posts: 734
Thanks: 0
Thanked 20 Times in 19 Posts
Aradon is on a distinguished road
Quote:
Originally Posted by BubikolRamios View Post
want to clear some basic things. Coding in fashion that any of my functions called returns true or false(some kind of error happed)

1. does "finally {varname = null;" have any effect at all or should I trigger
garbage collector from code or what, and if so , how ?
2. the code below in case of err returns false, so finaly is not executed
but should be, how to code that nicely, of course i could invent new
variable and then if wariable = then return ... but this is ugly.
3. Logger.getLogger(classname .., where does this go, this is automaticaly
generated by ide, and have no idea where does it go ?
4. is there a way to ask in finaly part how I got there, by errror or by no error ?

Code:
 public  static boolean someFunc() 
 { 
   type varname = null;
   try
   {         
      type varname = new type();     
      // ....
   }
   catch (type ex)
   {
      Logger.getLogger(classname.class.getName()).log(Level.SEVERE, null, ex);
      return false;
   }
   finally 
   {         
     varname = null;
   }
   return true;
}
Thaks for replays.
Let's discuss a little about catch, try, and finally.

a try catch block is typically called when you want to do some sort of error checking. This is seen the most when you are doing input and output as well as just general sanity checks (Maybe I want something to happen in my code no matter what happens).

Typically a try catch block looks like this:

Code:
public class testCatch
{
  public static void main(String args[])
  {
    try{
       // do something
    }
    catch(Exception e)
    {
       e.printStackTrace();
    }
  }
}
In that little piece of code, if something goes wrong in the try block, then the catch block will catch the exception (It should be noted that Exception is the parent class to all exceptions and thusly this catch will catch every exception thrown)

Now if I wanted something to happen no matter what I would create a finally block. The finally block is used to make sure that no matter what happens, io failure, problems with the code at runtime, etc, that the code inside the block will be ran.

Now, looking at the previous statements, let's answer your questions.

1) As stated, finally { varname = null; } will put the varname to null no matter what happens. This seems like you are either from c++ or the person who suggested this is. With Java, garbage collection is taken care of for you so you don't have to worry about it. Thusly, this statement will always be run, but unless you are concerned about setting it later, it doesn't effect the program too much.

2) Indeed, if you return from the catch block then you can expect finally not to be run. But if you are returning from the catch block, then what is the point of finally? Of course, looking at number 1, there doesn't seem to be much use for finally in this case anyways.

But let's say you wanted to run something in finally, then yes, you would have to create some type of variable to set and check. So instead of returning just true or false, you would return the setting of that variable instead (which may look ugly now, but is used a lot).

3) Typically the log files go into the specific location that the ide has specified. I, myself, don't use this. While this would be useful in an end program if you have all the log's set up correctly, when just creating a program and debugging I typically use either the printstacktrace, or system.out.println's to get what I want. After all, if my program is throwing an error while I'm writing code, typically it's not cause I want it to.

And if I do want it to, I want immediate results!

4) You can get to finally no matter what if you do not return in either the try or catch statement. In closing you would have to do something like this:

Code:
 public static boolean someFunc() 
 { 
   type varname = null;
   boolean ret = true;
   try
   {         
      type varname = new type();     
      // ....
   }
   catch (Exceptiontype ex)
   {
      Logger.getLogger(classname.class.getName()).log(Level.SEVERE, null, ex);
      ret = false;
   }
   finally 
   {         
     varname = null;
   }
   return ret;
}
Note, that I edited slightly two things in the code that you provided. I added a variable and changed the exception type for completeness.

Hope this helps!
__________________
"To iterate is human, to recurse divine." -L. Peter Deutsch
Aradon is offline   Reply With Quote
Old 03-09-2008, 06:54 PM   PM User | #3
BubikolRamios
Senior Coder

 
Join Date: Dec 2005
Location: Slovenia
Posts: 1,876
Thanks: 114
Thanked 76 Times in 76 Posts
BubikolRamios is on a distinguished road
Quote:
1) As stated, finally { varname = null; } will put the varname to null no matter what happens. This seems like you are either from c++ or the person who suggested this is. With Java, garbage collection is taken care of for you so you don't have to worry about it. Thusly, this statement will always be run, but unless you are concerned about setting it later, it doesn't effect the program too much.
to explain better:
1.having IDE net beans, in runtime there is a field in IDE tool bar
showing something like 141/245 mb, and tool tip says: click to force garbage collection
2. I'm assuming this is eating my computer RAM
3. Coming from pascal syntax there was allways good if not esential to do:
freeandnil(someobj) hence ..... = null in my first post.
4. does varname = null; frees computer RAM ?

I'm filling a lot insecurity not setting everything to null, when I don't need it any more, m'I wrong ?
BubikolRamios is offline   Reply With Quote
Old 03-09-2008, 08:17 PM   PM User | #4
Aradon
Moderator-san


 
Aradon's Avatar
 
Join Date: Jun 2005
Location: USA
Posts: 734
Thanks: 0
Thanked 20 Times in 19 Posts
Aradon is on a distinguished road
Let me put it this way. Once that function exits, all objects that are no longer referenced through a pointer are marked for garbage collection. Now typically this means that the memory is free'd and depending on the algorithm that is used (The default is the lovely copy algorithm) that memory is either free'd right away or marked for future use.

Let me go through your questions again and then at the end I will link an article you may want to read up on for a bit more information on garbage collection.

1) Well since netbeans is built by Sun, I have no doubt that they can force the particular algorithm that is being used to do garbage collection, however typically you as a programmer don't have control over this. All you can do is create a method to execute when garbage collection is run. And while there may be uses for that, I'm unsure off the top of my head what it could be.

2) Well yes and no. Once a function exits it comes up for garbage collection if those variables are no longer in scope. Basically in java you have to rely on the JVM to keep track of memory for you. Something that may seem odd coming into it.

3) It's not essential, nor is it needed. You could just create the variable and then use it right then. (of course this is different when you are creating Classes, but that is for another lesson).

4) No, it doesn't. All it states is that you are setting the particular address of memory to null. But the memory is still allocated for that variable. So you are freeing up nothing.

Here is a pretty awesome article on basic garbage collection:

Java Cert

Here is a more advanced article on the garbage collection in java:

Java World

And lastly, how you can tell if a particular class is run through garbage collection:

tipsmart
__________________
"To iterate is human, to recurse divine." -L. Peter Deutsch
Aradon is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:00 PM.


Advertisement
Log in to turn off these ads.