Quote:
Originally Posted by BubikolRamios
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!