4 ms·
Often an assert macro can be written using throw/raise as the mechanism but it changes the syntax and reduces some boiler plate. For example: try {
by reggieband 8y ago
Often an assert macro can be written using throw/raise as the mechanism but it changes the syntax and reduces some boiler plate.
For example:
try {
someVar.someMethod(params);
} catch (e) {
throw new WhateverError("More detail here", e);
}
becomes:
assert(someVar != null, "Unexpected null someVar");
assertAndContinue(params.val > 0, "Expected greater than 0 val", params.val);
// etc.
someVar.someMethod(params);
It may not look very different but it adds a documentation like effect to the code. It lays out the potential failure cases before the code execution. Even if internally `assert` becomes `throw`.
- wvenable 8y agoBit what assertAndContinue and not just assert in the second line? What value is there in continuing in that case?
- aaaaaaaaaab 8y agoSo that managers can show pretty charts on board meetings about low crash numbers.
- wvenable 8y agoJust add a top-level exception handler that captures and logs everything but then restarts main() and never crash again! :)
- reggieband 8y agoIn some cases there are suspicious values that aren't necessarily incorrect. The person writing the code might think: "I don't expect this value but it won't cause an exception in the way a null reference will". You may want to catch these in development and if they show up, you investigate and there is no actual problem with the value then you can update the assertion to more accurately reflect the data.