Is it better, less expensive or more readable to use a try/catch
block in Java instead of using multiple If
statements to check user input for example?
Example when parsing a Date
string, won't it be better to parse directly using a try/catch block instead of writing multiple statements looking for illegal characters.
In another example, say i want to read a file or stream, instead of using Scanner
, I just force the method and wait for an exception to occur.
Is that a healthy method of programming? Is it less expensive on the Virtual Machine?
UPDATE
Here is an example of what i meant when using DateFormat exception, sometimes it could be a real problem to catch an error, and when doing so, can you guarantee your complicated (usually unreadable) code is error prone?