throw 문
try...catch...finally 문에서 처리할 수 있는 오류 조건을 만듭니다.
throw [exception]
인수
- 예외
선택적 요소로서, 임의의 식입니다.
설명
throw 문은 catch 블록 내부에 있을 경우에만 인수 없이 사용할 수 있습니다. 이 경우 throw 문은 자신을 포함하는 catch 문이 catch한 오류를 다시 throw합니다. 인수가 제공되면 throw 문은 exception 값을 throw합니다.
예제
다음 예제에서는 전달된 값을 기준으로 오류를 throw한 후try...catch...finally 문 계층 구조에서 오류를 처리하는 방법을 보여 줍니다.
function ThrowDemo(x)
{
try
{
try
{
// Throw an exception that depends on the argument.
if (x == 0)
throw new Error(200, "x equals zero");
else
throw new Error(201, "x does not equal zero");
}
catch(e)
{
// Handle the exception.
switch (e.number)
{
case 200:
print (e.message + " - handled locally.");
break;
default:
// Throw the exception to a higher level.
throw e;
}
}
}
catch(e)
{
// Handle the higher-level exception.
print (e.message + " - handled higher up.");
}
}
ThrowDemo (0);
ThrowDemo (1);
// Output:
// x equals zero - handled locally.
// x does not equal zero - handled higher up.