You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/08-error-handling/1-try-catch/article.md
+30-28Lines changed: 30 additions & 28 deletions
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,14 @@
1
1
# Error handling, "try..catch"
2
2
3
-
No matter how great we are at programming, sometimes our scripts have errors. They may occur because of our mistakes, an unexpected user input, an erroneous server response and for a thousand of other reasons.
3
+
아무리 프로그래밍에 능하더라도, 때때로 스크립트에는 오류가 있습니다. 원인은 아마도 실수이거나, 예상치 못한 사용자 입력이나 잘못된 서버 응답, 그 밖에 수천만가지 다른 이유들로 인한 것입니다.
4
4
5
-
Usually, a script "dies" (immediately stops) in case of an error, printing it to console.
5
+
보통, 오류인 경우에 스크립트는 "죽으면서" (즉시 중단되면서), 콘솔에 오류를 출력합니다.
6
6
7
-
But there's a syntax construct `try..catch`that allows to "catch" errors and, instead of dying, do something more reasonable.
7
+
그러나 `try..catch`문법을 사용하면 오류를 "잡아서", 죽는 대신에 더 합당한 무언가를 할 수 있게 됩니다.
8
8
9
-
## The "try..catch" syntax
9
+
## "try..catch" 문법
10
10
11
-
The `try..catch` construct has two main blocks: `try`, and then `catch`:
11
+
'try..catch' 구문에는 두 개의 주요 블록이 있습니다: 'try'와 'catch'입니다.
12
12
13
13
```js
14
14
try {
@@ -22,19 +22,19 @@ try {
22
22
}
23
23
```
24
24
25
-
It works like this:
25
+
이는 다음처럼 동작합니다.
26
26
27
-
1.First, the code in `try {...}`is executed.
28
-
2.If there were no errors, then `catch(err)` is ignored: the execution reaches the end of `try` and then jumps over `catch`.
29
-
3.If an error occurs, then `try`execution is stopped, and the control flows to the beginning of `catch(err)`. The `err`variable (can use any name for it) contains an error object with details about what's happened.
27
+
1.먼저, `try {...}`안의 코드가 실행됩니다.
28
+
2.만약 오류가 없다면, `catch(err)`는 무시되고, `try`의 끝까지 실행되고나면 `catch`로 뛰어넘습니다.
29
+
3.만약 오류가 있으면, `try`실행이 중단되고, `catch(err)`의 첫부분으로 넘어갑니다. `err`변수(아무 이름이나 사용 가능)는 무슨 일이 일어났는지 상세를 담은 오류 개체를 포함합니다.
30
30
31
31

32
32
33
-
So, an error inside the `try {…}`block does not kill the script: we have a chance to handle it in `catch`.
33
+
그래서, `try {…}`블록 안쪽의 오류는 스크립트를 죽이지 않고, `catch`에서 다룰 수 있는 기회가 생깁니다.
34
34
35
-
Let's see more examples.
35
+
예제를 좀 더 봅시다.
36
36
37
-
-An errorless example: shows `alert``(1)` and `(2)`:
37
+
-오류가 없는 예제로, `alert``(1)`과 `(2)`를 보여줍니다.
38
38
39
39
```js run
40
40
try {
@@ -53,7 +53,7 @@ Let's see more examples.
53
53
54
54
alert("...Then the execution continues");
55
55
```
56
-
-An example with an error: shows `(1)` and `(3)`:
56
+
-오류가 있는 예제로, `(1)`과 `(3)`을 보여줍니다.
57
57
58
58
```js run
59
59
try {
@@ -76,10 +76,10 @@ Let's see more examples.
76
76
```
77
77
78
78
79
-
````warn header="`try..catch` only works for runtime errors"
80
-
For `try..catch` to work, the code must be runnable. In other words, it should be valid JavaScript.
79
+
````warn header="`try..catch`는 오직 런타임 오류에만 동작합니다."
80
+
`try..catch`가 동작하려면, 코드가 실행 가능해야 합니다. 다시 말하면, 유효한 자바스크립트여야 합니다.
81
81
82
-
It won't work if the code is syntactically wrong, for instance it has unmatched curly braces:
82
+
만약 코드가 문법적으로 틀리면 동작하지 않을 것입니다. 중괄호 짝이 안 맞는 것을 예로 들 수 있습니다.
83
83
84
84
```js run
85
85
try {
@@ -89,14 +89,14 @@ try {
89
89
}
90
90
```
91
91
92
-
The JavaScript engine first reads the code, and then runs it. The errors that occur on the reading phrase are called "parse-time" errors and are unrecoverable (from inside that code). That's because the engine can't understand the code.
92
+
자바스크립트 엔진은 먼저 코드를 읽고, 그 다음에 실행합니다. 구문을 읽는 중에 발생하는 오류는 "parse-time"오류라고 부르며 (그 코드 안에서) 복구 불가능합니다. 엔진이 코드를 이해할 수 없기 때문입니다.
93
93
94
-
So, `try..catch` can only handle errors that occur in the valid code. Such errors are called "runtime errors" or, sometimes, "exceptions".
94
+
따라서, `try..catch`는 유효한 코드에서 발생하는 오류만 처리할 수 있습니다. 이런 오류들을 "런타임 오류" 또는 가끔씩 "예외"라고 부릅니다.
95
95
````
96
96
97
97
98
-
````warn header="`try..catch` works synchronously"
99
-
If an exception happens in "scheduled" code, like in `setTimeout`, then `try..catch` won't catch it:
98
+
````warn header="`try..catch`는 동기적으로 동작합니다"
99
+
만약 예외가 setTimeout처럼 "스케줄된" 코드에서 발생한다면`try..catch`가 잡아낼 수 없을 것입니다.
100
100
101
101
```js run
102
102
try {
@@ -109,8 +109,10 @@ try {
109
109
```
110
110
111
111
That's because `try..catch` actually wraps the `setTimeout` call that schedules the function. But the function itself is executed later, when the engine has already left the `try..catch` construct.
112
+
왜냐하면 `try..catch`는 실제로는 함수를 스케줄하는 `setTimeout` 호출을 감싸고 있는 것이기 때문입니다. 하지만 함수 자체는 엔진이 `try..catch` 구문을 떠나버린 나중 시점에 실행됩니다.
113
+
114
+
스케줄된 함수 내부의 예외를 잡으려면, `try..catch`가 함수 내부에 있어야 합니다.
112
115
113
-
To catch an exception inside a scheduled function, `try..catch` must be inside that function:
114
116
```js run
115
117
setTimeout(function() {
116
118
try {
@@ -122,9 +124,9 @@ setTimeout(function() {
122
124
```
123
125
````
124
126
125
-
## Error object
127
+
## 오류 개체
126
128
127
-
When an error occurs, JavaScript generates an object containing the details about it. The object is then passed as an argument to `catch`:
129
+
오류가 일어나면, 자바스크립트는 오류 상세를 포함한 개체를 만듭니다. 개체는 그 후 `catch`에 인수로서 전달됩니다.
128
130
129
131
```js
130
132
try {
@@ -134,18 +136,18 @@ try {
134
136
}
135
137
```
136
138
137
-
For all built-in errors, the error object inside `catch` block has two main properties:
139
+
모든 내장 오류에 대해서, `catch` 블록 내부의 오류 개체는 두 주요 속성을 가집니다.
138
140
139
141
`name`
140
-
: Error name. For an undefined variable that's `"ReferenceError"`.
142
+
: 오류 이름. 정의되지 않은 변수에 대해서는 "참조오류".
141
143
142
144
`message`
143
-
:Textual message about error details.
145
+
: 오류 상세에 대한 글 메시지.
144
146
145
-
There are other non-standard properties available in most environments. Oneof most widely used and supported is:
147
+
그 밖에 대부분의 환경에서 가능한 비표준 속성들이 있습니다. 가장 널리 사용되고 지원되는 것으로는 다음이 있습니다.
146
148
147
149
`stack`
148
-
:Current call stack: a string with information about the sequence of nested calls that led to the error. Usedfor debugging purposes.
150
+
: 현재 호출 스택: 오류로 이어지는 중첩 호출의 나열에 대한 정보를 가진 문자열. 디버깅 목적으로 사용.
0 commit comments