Section 10. Error
2395 ワード
Udemy - JavaScript: The Advanced Concepts
new Error()自体は何の仕事をしているわけではありません.
投げてこそ意味がある
throw:運転を停止し、例外を投げ出します.
errorだけでなく、何でも投げられます
->現在の実行コンテキストの中止
->呼び出しスタックに沿って、次のコンテキストのcatchの場所を検索して制御します.
(->catchが常に存在しない場合は、プログラムを終了する必要があります)
->runtimeキャプチャ(onerror)/process.on("uncaughtException"))
name: 'Error'
Message:new Error()時にパラメータのstringに入る
stack:エラーが発生した場所を示す文字列
(多くの情報を露出したくない、あるいは共同で使いたい、など)
try{} catch{} finally{} :
同期コード+非同期コード(Async/Await)
tryが戻ってもfinallyは実行されます
catch():非同期コード(Promise)
初志に影響しない
中間承諾のエラーをキャプチャするには、中間承諾のcatch()に接続する必要があります.
catchのパラメータ(ここではerr)はcatch blockでのみ有効です
Section 10. Error
Throw Error
new Error()自体は何の仕事をしているわけではありません.
投げてこそ意味がある
throw:運転を停止し、例外を投げ出します.
errorだけでなく、何でも投げられます
//이런것도 가능!!
try {
throw 'hi'
} catch (e) {
console.log(e); // 'hi'
}
呼び出しthrow->現在の実行コンテキストの中止
->呼び出しスタックに沿って、次のコンテキストのcatchの場所を検索して制御します.
(->catchが常に存在しない場合は、プログラムを終了する必要があります)
->runtimeキャプチャ(onerror)/process.on("uncaughtException"))
Errorプロパティ
name: 'Error'
Message:new Error()時にパラメータのstringに入る
stack:エラーが発生した場所を示す文字列
// 이런것도 가능!!
const myError = new Error('This is message');
myError.name = 'HAHAHA';
console.log(myError);
//HAHAHA: This is message
// at Object.<anonymous> (/Users/wendy.wh/develop/free/free.js:1:17)
Errorをカスタマイズして使用(多くの情報を露出したくない、あるいは共同で使いたい、など)
class AuthenticationError extends Error {
constructor(message) {
super(message)
this.name = 'AuthenticationError'
}
}
throw new AuthenticationError('내가 만든 에러~');
Handle Error
try{} catch{} finally{} :
同期コード+非同期コード(Async/Await)
tryが戻ってもfinallyは実行されます
catch():非同期コード(Promise)
Promise
.resolve('resolved')
.then(res => {
const newRes = res + ' 1';
console.log(newRes);
Promise.resolve().then(res => { throw Error('new Error!!!') });
return newRes;
})
.then(res => {
const newRes = res + ' 2';
console.log(newRes);
return newRes;
})
// resolved 1
// resolved 1 2
// UnhandledPromiseRejectionWarning: Error: new Error!!!
上記の状況は、中間承諾でエラーが発生しても、初志に影響しない
中間承諾のエラーをキャプチャするには、中間承諾のcatch()に接続する必要があります.
catch block scope
catchのパラメータ(ここではerr)はcatch blockでのみ有効です
(function () {
try {
throw new Error();
} catch (err) {
var err = 5;
var boo = 10;
console.log(err); //5
}
console.log(err); //undefined
console.log(boo); //10
})();
Reference
この問題について(Section 10. Error), 我々は、より多くの情報をここで見つけました https://velog.io/@dldngus5/Section10-Errorテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol