1.
DON’T USE TRY-CATCH
Tetsuharu OHZEKI aka @saneyuki_s
April 5, 2016. 東京node学園 #20
2.
ABOUT ME
• Tetsuharu OHZEKI
• twitter: saneyuki_s
• Open Source Contributor
• Mozilla Committer
• Firefox, Servo (as reviewer)
• ReactiveX/RxJS
• And some others…
• whatwg html, etc
• Working atVOYAGE GROUP, inc.
• My main work is to develop an advertising management system with using
TypeScript, babel compiler, React, and RxJS.
5.
TWO TYPES OF “ERROR”
•Just a Bug
• NullPointerException,TypeError, calling undefined methods, not found a
required file, etc...
•Recoverable Errors
• Network timeout, JSON parse errors, etc…
6.
CAN YOU RECOVER A BUG
IN A RUNNING APPLICATION?
No
• A bug is just a programming mistake.
• Don’t recover them.
• So “let it crash”,“abandonment”,“fail-fast” immediately.
• Even if you success recovering from their fatal,
there’re some serious conformity problem in your heap.
7.
Promise/Observable.catch() are any type in
TypeScript definitions. This is a hole for type
restriction.
interface Promise<T> {
catch(onrejected?: (reason: any) => T | PromiseLike<T>):
Promise<T>;
catch(onrejected?: (reason: any) => void): Promise<T>;
}
interface Observable<T> {
catch<R>(selector: (err: any, caught: Observable<T>) =>
Observable<R>): Observable<R>;
}
8.
CAN WE USE TRY-CATCH FOR
RECOVERABLE?
• I don’t think so excluding some exceptions…
• (of course, we have to handle errors from builtin
object, dom, libraries, ete with try-catch)
• “error throw-ablity” should be restricted by a
function signature.
• should force an error handling for a caller!
9.
function parseJSON(obj) {
try {
const value = JSON.parse(obj);
return value;
}
catch (e) {
return null;
}
}
17.
RESULT<T, E>
• Result<T, E> == Either<A, B>
• You can use it from npm:option-t
• APIs are inspired by Rustlang’s std::result
• There are some instance methods
• This provides “utility” object & methods.
• You can same things with destructuring assignment.
18.
const result = getSomeResult();
if ( result.isOk() ) {
// success
}
else {
// error
}
19.
const result = getSomeResult();
const value: string = result
.map((v: number) => String(v)).unwrap();
const first = getSomeResult();
const second = first.andThen(first => {
return getSecondResult(first);
});
21.
SEE
HTTPS://DOC.RUST-LANG.ORG/BOOK/ERROR-
HANDLING.HTML
22.
DRAWBACKS IN JAVASCRIPT
• Need a extra object allocation cost
• In the future, JavaScript runtime may make the cost more cheap.
• Don’t use them if you require computation performance
extremely.
• Result<T, E> type needs static type analytics for more
convenient/secure programming (but plain JavaScript is not so).
• Let’s use withTypeScript or Flowtype
23.
•DO NOT TRY TO RECOVER A BUG
• Don’t suppress a bug. FIX it.
•Don’t use try-catch
• Excluding: Runtime provided functions (include JSON.parse, DOM),ssand 3rd
party libraries.
•Tell “Error-Throwable” via function signature.
Clipping is a handy way to collect and organize the most important slides from a presentation. You can keep your great finds in clipboards organized around topics.
Be the first to comment