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?
It’s a bad practice to use Exceptions for a flow control.
Some quotes:
Update:
As it’s said (and I remember that I also found the same statement in Josh Bloch’s blog) using exceptions for flow control is like using GOTOs for the same purpose. You can read interesting Dijkstra article about why GOTOs are bad.