So, I have something where I want, in the try block, add various data to some data object, and then in case of an exception, save this record with an error and all data fields received before the exception. In Java, this will be easy. Even if you go with some kind of immutable record type, you can do:
MyRecord record = new MyRecord();
try {
record = record.withA(dangerouslyGetA());
record = record.withB(dangerouslyGetB());
record = record.withC(dangerouslyGetC());
} catch (Exception ex) {
record = record.withError(ex);
}
save(record);
So, if he bombes in step C, he will save the record with A, B and error.
I cannot find an easy way to do this in Clojure. If you put try
around a let
, then you must assign an “update” record to the new variables each, and therefore they do not fall within the scope of the expression catch
. And even if they were, you would not know which one to use.
I think I could put try / catch / let around each expression, but this is a lot more code than the Java version and requires duplication of the statement save
everywhere. My understanding was that Clojure was great for his patience and easy avoidance of duplication, so something makes me think this is the wrong way.
Of course, this is a fairly common need and has a simple idiomatic solution, right?
source
share