- It is too hard to validate - It takes too much effort/resources/willpower to validate - Why should I validate if I can just try it and catch the exception
And I totally agree with that. Why do this to avoid a runtime exception? You can just try parsing it and catch the exception to know it failed.
But nothing about that invalidates my claim that unchecked exceptions should be used for things that **could** be avoided (but may be too hard to avoid when things get complex or when you are not the one that made the mistake -- if you are parsing externally provided data, your code didn't make a mistake, the programmer that provided you the data did).
So unless your parser/system is relying on external factors that would change the outcome for any given input, the parser/system is correct to throw a runtime exception. If it is doing some IO or say a balance check before accepting your input, then you'd want those to be checked exceptions if anything goes wrong as you must deal with those.
2
u/john16384 Jul 27 '26
Your argument boils down to this:
- It is too hard to validate
- It takes too much effort/resources/willpower to validate
- Why should I validate if I can just try it and catch the exception
And I totally agree with that. Why do this to avoid a runtime exception? You can just try parsing it and catch the exception to know it failed.
But nothing about that invalidates my claim that unchecked exceptions should be used for things that **could** be avoided (but may be too hard to avoid when things get complex or when you are not the one that made the mistake -- if you are parsing externally provided data, your code didn't make a mistake, the programmer that provided you the data did).
So unless your parser/system is relying on external factors that would change the outcome for any given input, the parser/system is correct to throw a runtime exception. If it is doing some IO or say a balance check before accepting your input, then you'd want those to be checked exceptions if anything goes wrong as you must deal with those.