I am trying to use the short hand operator to check a condition in order to throw an exception.
throw (result == false) ? new Exception("Result is false.") : null;
The lines following the above line, I receive an "unreachable code detected error".
I'm thinking that if I "throw null" if the condition is not met, it is still throwing an exception.
Any better way to do this short hand?
As far as I know, you can't do this because you are throw
ing either an exception or a null and in either case the code below is always unreachable. You could simply do this on one line:
if (!result) throw new Exception("Result is false.");
This even ends up being less typed characters
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With