/* a calling b b calling c c :throwing Error Error: e error at c (<anonymous>:17:11) at b (<anonymous>:11:5) at a (<anonymous>:5:5) at <anonymous>:28:5 d calling c c :throwing Error Error: e error at c (<anonymous>:17:11) at d (<anonymous>:23:5) at <anonymous>:34:5 */
11.5 try…catch…finally
try블록의 코드가 HTTP 연결이나 파일 같은 ‘자원’을 처리해야 할 때 어느 시점에서 이 자원을 해제해야함
try는 에러가 일어나면 자원을 헤재할 기회가 사라질 수 있음
catch는 에러가 없으면 실행되지 않음
finally 에러 관계없이 반드시 호출됨
try...catch...finally
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//try...catch...finally try{ console.log("1.this line is executed.."); thrownewError("whoops"); console.log("2.this line is not.."); }catch(err){ console.log("3.there was an error..."); }finally{ console.log("4....always excuted"); } /* 1.this line is executed.. 3.there was an error... 4....always excuted */