-
Error HandlingJavaScript 2021. 12. 11. 22:59
자바스크립트에서 에러를 다루는 방법을 정리하였습니다.< Introduction to Error Handling >
We can’t always stop errors before they occur, but we can include a backup plan in our program to anticipate and respond to the errors to ensure that our program continues running.
Error handling is the process of programmatically anticipating and addressing errors.
< Runtime Errors >
Errors contain useful messages that tell us why our program isn’t working or why the error was thrown.
When an error is thrown, our program stops running and the console displays the error message.
When we execute code and a line of code throws an error, that error is referred to as a runtime error.< Error > Note : Javascript Error is object that have a name and message property.
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Error/name
Error.prototype.name - JavaScript | MDN
기본적으로 Error 인스턴스에는 "Error"라는 이름을 갖습니다. name 속성과 message 속성은 Error.prototype.toString() 메서드에서 오류의 문자열 표현을 생성하는 데 사용됩니다.
developer.mozilla.org
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/message
Error.prototype.message - JavaScript | MDN
The message property is a human-readable description of the error.
developer.mozilla.org
< Constructing an Error >
When we need an error that isn't covered aby the built-in errors.
Use Error( ) function.- The Error function takes an argument of a string which becomes the value of the error’s message property.
- Keep in mind that creating an error is not the same as throwing an error. A thrown error will cause the program to stop running, But creating an error doesn’t cause our program to stop
Example)
console.log(Error('Your password is too weak.')); // Prints: Error: Your password is too weak. // Error() === new Error() console.log(new Error('Your password is too weak.')); // Prints: Error: Your password is too weak.
< The throw Keyword >
To throw an error in JavaScript, use the throw keyword
Example)
throw Error('Something wrong happened'); // Error: Something wrong happened console.log('This will never run');
: When we use the throw keyword, the error is thrown and code after throw statement will not execute.
< The try...catch Statement >
To anticipate and handle errors and allow our program to continue running.
try { // try block } catch (thrown error) { // catch block }
1. try block : evaluate the code in try block to anticipate errors.
2. catch block : handle the error in catch block and allow program to continue running.
-> in a try...catch statement, we evaluate code in the try block and if the code throws an error, the code inside the catch block will handle the error for us.
Example)
const someVar = 'Cannot be reassigned'; try { someVar = 'Still going to try'; } catch (e) { console.log(e); } // TypeError : Assignment to constant variable.
'JavaScript' 카테고리의 다른 글
Promise (0) 2021.12.13 Asynchronous / Event Loop (0) 2021.12.12 Modules (0) 2021.12.11 Classes (0) 2021.12.10 Data type (0) 2021.10.25