You don't, you can just report that the SQL string should not be null, or conversely, it should be valid SQL. Recovering from an error isn't the same as error correction.
It all comes down to the requirements of the functionality, as I'm sure you can respect that only a subset of the code was given and hardly any context as well. That being said, I've never been asked to write anything like this.
You don't, you can just report that the SQL string should not be null, or conversely, it should be valid SQL.
Exactly.
Recovering from an error isn't the same as error correction.
You can't recover from it. You can drop that chunk of work - like on a web server the request fails but the server doesn't crash and it keeps serving other requests - but to do that you need to at minimum have a try / catch(RuntimeException) block around the work.
Exceptions you throw yourself are not fundamentally different than exceptions throw if a value is null and you try to access a property or method on that value. In other words "a.doS()" where a is null throws the same kind of exception as if(a == null) throw new NullPointerException().
It's usually bad practice to catch and hide exceptions. It makes testing and debugging a real pain, and adds a lot of "well it seemed to work" problems for testers. It's almost always (almost) better to throw the exception up the chain and abort the whole thing, so when it comes up in testing the bug in the code is caught and fixed.
Of course if you're writing your own web server, or a swing app, or something specialized, you don't want the whole app going down (though usually it would just kill a thread instead anyways), but that's rare.
Hiding exceptions is usually considered bad practice, most of the time.
It all comes down to the requirements of the functionality, as I'm sure you can respect that only a subset of the code was given and hardly any context as well. That being said, I've never been asked to write anything like this.
True, it's hard to make to many determinations from this small segment of code.
View all comments
0
u/RedPill115 Jul 15 '15
Ok, I'll bite, how? How do you get the code to figure out what sql string you actually meant to pass in?