3 ms·
Also, is doing flow control using exceptions an acceptable pattern in the OCaml world? Like in Java: try { print(awesomeObj.getSomeState()); } catch(
by ignoramous 12y ago
Also, is doing flow control using exceptions an acceptable pattern in the OCaml world?
Like in Java:
try {
print(awesomeObj.getSomeState());
} catch(NullPointerException npe) {
awesomeObj = AwesomeObj.getInstance();
}
would be considered very bad. You would instead be expected to do:
if (awesomeObj == null) awesomeObj = AwesomeObj.getInstance();
print(awesomeObj.getSomeState());
(how do you <code-wrap> stuff on here?)
- gsg 12y agoYes. Various parts of the stdlib are intended to be used by catching Not_found or End_of_file or whichever exception happens to be relevant. It's not what I would have done. (Mark text as code by putting it on a fresh line with two or more spaces in front. I don't think there is a way to do this inline.)
- reblochon 12y agoWhile being done in the standard library, several big libraries alternative (Core, Batteries...) prefer wrapping everything in an `option` type instead of raising an exception. Wrapping in an option avoids forgetting about catching the exception.
- kenko 12y agoIt's always weird to me when people talk about using exceptions for control flow. Exceptions are, pretty much explicitly, a control flow mechanism. That's what they do, they transfer control from one place to another. It's like people are getting hung up on the name.
- ignoramous 12y agoExceptions, in Java land at least, are very costly (think interrupts and context switching). To do flow control using them is shooting yourself in the foot.