Throw 정보
간단한 설명
종료 오류를 생성하는 Throw 키워드에 대해 설명합니다.
자세한 설명
throw 키워드(keyword) 종료 오류가 발생합니다. throw 키워드(keyword) 사용하여 명령, 함수 또는 스크립트의 처리를 중지할 수 있습니다.
예를 들어 If 문의 스크립트 블록에 throw 키워드(keyword) 사용하여 조건에 응답하거나 Try-Catch-Finally 문의 Catch 블록에 응답할 수 있습니다. 매개 변수 선언에서 throw 키워드(keyword) 사용하여 함수 매개 변수를 필수로 만들 수도 있습니다.
throw 키워드(keyword) 사용자 메시지 문자열 또는 오류를 발생시킨 개체와 같은 모든 개체를 throw할 수 있습니다.
SYNTAX
throw 키워드(keyword) 구문은 다음과 같습니다.
throw [<expression>]
Throw 구문의 식은 선택 사항입니다. Throw 문이 Catch 블록에 나타나지 않고 식을 포함하지 않으면 ScriptHalted 오류가 생성됩니다.
C:\PS> throw
ScriptHalted
At line:1 char:6
+ throw <<<<
+ CategoryInfo : OperationStopped: (:) [], RuntimeException
+ FullyQualifiedErrorId : ScriptHalted
throw 키워드(keyword) 식 없이 Catch 블록에서 사용되는 경우 현재 RuntimeException을 다시 throw합니다. 자세한 내용은 about_Try_Catch_Finally 참조하세요.
문자열 throw
다음 예제와 같이 Throw 문의 선택적 식은 문자열일 수 있습니다.
C:\PS> throw "This is an error."
This is an error.
At line:1 char:6
+ throw <<<< "This is an error."
+ CategoryInfo : OperationStopped: (This is an error.:String) [], R
untimeException
+ FullyQualifiedErrorId : This is an error.
다른 개체 throw
식은 다음 예제와 같이 PowerShell 프로세스를 나타내는 개체를 throw하는 개체일 수도 있습니다.
C:\PS> throw (get-process PowerShell)
System.Diagnostics.Process (PowerShell)
At line:1 char:6
+ throw <<<< (get-process PowerShell)
+ CategoryInfo : OperationStopped: (System.Diagnostics.Process (Pow
erShell):Process) [],
RuntimeException
+ FullyQualifiedErrorId : System.Diagnostics.Process (PowerShell)
$error 자동 변수에서 ErrorRecord 개체의 TargetObject 속성을 사용하여 오류를 검사할 수 있습니다.
C:\PS> $error[0].targetobject
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
319 26 61016 70864 568 3.28 5548 PowerShell
ErrorRecord 개체 또는 Microsoft .NET Framework 예외를 throw할 수도 있습니다. 다음 예제에서는 Throw 키워드(keyword) 사용하여 System.FormatException 개체를 throw합니다.
C:\PS> $formatError = new-object system.formatexception
C:\PS> throw $formatError
One of the identified items was in an invalid format.
At line:1 char:6
+ throw <<<< $formatError
+ CategoryInfo : OperationStopped: (:) [], FormatException
+ FullyQualifiedErrorId : One of the identified items was in an invalid
format.
결과 오류
Throw 키워드(keyword) ErrorRecord 개체를 생성할 수 있습니다. ErrorRecord 개체의 Exception 속성에는 RuntimeException 개체가 포함됩니다. ErrorRecord 개체의 나머지와 RuntimeException 개체는 Throw 키워드(keyword) throw하는 개체에 따라 다릅니다.
RunTimeException 개체는 ErrorRecord 개체에 래핑되고 ErrorRecord 개체는 자동으로 $Error 자동 변수에 저장됩니다.
THROW를 사용하여 필수 매개 변수 만들기
throw 키워드(keyword) 사용하여 함수 매개 변수를 필수로 만들 수 있습니다.
매개 변수 키워드(keyword) 필수 매개 변수를 사용하는 대신 사용할 수 있습니다. 필수 매개 변수를 사용하는 경우 시스템에서 사용자에게 필요한 매개 변수 값을 묻는 메시지를 표시합니다. throw 키워드(keyword) 사용하면 명령이 중지되고 오류 레코드가 표시됩니다.
예를 들어 매개 변수 하위 식의 throw 키워드(keyword) Path 매개 변수를 함수의 필수 매개 변수로 만듭니다.
이 경우 Throw 키워드(keyword) 메시지 문자열을 throw하지만 Path 매개 변수가 지정되지 않은 경우 종료 오류를 생성하는 throw 키워드(keyword) 존재합니다. Throw 뒤에 있는 식은 선택 사항입니다.
function Get-XMLFiles
{
param ($path = $(throw "The Path parameter is required."))
dir -path $path\*.xml -recurse |
sort lastwritetime |
ft lastwritetime, attributes, name -auto
}