Re: Try/Catch
- Posted by jimcbrown (admin) Feb 12, 2015
- 4218 views
The best value is the correct value that the programmer intended. If try/catch can magically know what that is then Great!
The reason that it knows that is because the programmer has to explicitly set the value in the catch statement - there's no magic to it.
But that just begs the question of why the programmer didn't correct the logic error that led to the exception being raised in the first place!
Err - http://en.wikipedia.org/wiki/Begging_the_question
I'm going to rewrite that as:
But that just asks the question of why the programmer didn't correct the logic error that led to the exception being raised in the first place!
Probably for the same reasons that some callers of open() check the return value to see if it's -1, instead of trying to prove that the file exists and permission to read it is granted before calling open() and then gets().
"they all share the same problem" - not stated what this is
The problem is that whatever value that ends up in the variable, it's not obvious that the variable has now been set to this. It's also not necessarily obvious that this happened at all (that the variable got a "default" value instead of having the operation be successful and getting a real value back).
"whereas, try/catch doesn't" - not stated how this is any better
This doesn't have the issue because one generally sets the variable explicitly in this case. Also, the flow of execution is deliberately interrupted here, which makes it clearer that something went wrong.
This is just handwaving.
Spock
No, it isn't.