수정 된 예외에`throw;`사용
예외를 foo
던질 수 있는 함수 가 있습니다 bar
.
다른 함수에서 호출 foo
하지만 bar
throw 되는 경우 예외 에 대한 세부 정보를 추가 할 수 있습니다 . ( foo
그 함수의 일반적인 특성으로 인해 실제로 거기에 속하지 않기 때문에 매개 변수와 같은 정보를 전달 하지 않습니다.)
그래서 나는 호출자에서 이것을합니다.
try {
foo();
} catch (bar& ex){
ex.addSomeMoreInformation(...);
throw;
}
윌 throw
수정 된 예외를 다시 던지거나 내가 사용해야합니까 throw ex;
? 후자는 아마도 가치 사본을 취할 것이기 때문에 나는 그렇게하지 않을 것입니다. 시겠습니까 throw
너무 값의 사본을? 나는 그렇지 않을 것이라고 생각한다.
(내가 확인할 수 있다는 것을 알고 있지만 지정되지 않거나 정의되지 않은 구조에 걸려 넘어지는 것에 대해 걱정하므로 확실히 알고 싶습니다.)
C ++ 11 §15.1 / 8 :
" 스로인 식 없이 피연산자 현재 예외 처리 (15.3) rethrows. 예외는 기존 임시로 다시 활성화됩니다. 새로운 임시 예외 개체가 생성되지 않습니다.
실제로 표준은 여기에서 매우 정확합니다. [핸들 제외] / 17 :
핸들러가 상수가 아닌 객체에 대한 참조를 선언 할 때 참조 된 객체에 대한 모든 변경 사항은 throw-expression 이 실행될 때 초기화 된 임시 객체의 변경 사항 이며 해당 객체가 다시 throw 될 때 적용됩니다 .
[except.throw] / 8 :
던져 표현 하는 피연 j는 현재 처리 된 예외 (15.3) rethrows.
이 경우 throw
원하는 동작을 얻기 위해 사용해야 합니다. 즉, 예외가 참조에 의해 포착되었으므로 throw는 수정 된 예외를 throw합니다.
예제를 통해 명시 적으로 던지는 차이점을 만들어 보겠습니다.
class exception
{
};
class MyException : public exception
{
};
void func()
{
try
{
throw MyException();
}
catch( exception& e )
{
//do some modification.
throw; //Statement_1
throw e; //Statement_2
}
}
Statment_1 :-
throw가하는 일은 현재 예외가 무엇인지 다시 던지는 것입니다. 즉, 더 이상 복사본을 만들지 않습니다 (예외가 처음에 throw되었을 때 만든 것처럼). 따라서 여기서 포착 된 예외를 변경하면 호출자 루틴에도있을 것입니다.
Statement_2 :-
이것은 원래 MyException으로 잡힌 "예외"를 던지고 있습니다. 즉, 복사본을 다시 만들 것입니다. 따라서 변경 사항을 잊어 버리면 호출자에게 또는 기본 예외도 전달되지 않습니다. 호출자 루틴에 "예외"를 던집니다.
내가 충분히 명확하기를 바랍니다 (그리고 C ++ 표준의 트랙에 대한 권리) ...
throw
(예외 객체없이) 현재 예외를 다시 발생시킵니다. (캐치 블록 안에 있어야합니다. 그렇지 않으면 std :: terminate 가 호출됩니다). 현재 예외 객체의 참조를 변경했기 때문에 명시 적으로 객체를 던지고 수정 된 예외를 다시 던질 필요가 없으며 새 임시 객체가 생성되지 않습니다.
에 따라 이 , C에서 예외를 던지는 ++ 두 가지 방법으로 수행 할 수 있습니다 :
- throw expression: First, copy-initializes the exception object from expression (this may call the move constructor for rvalue expression, and the copy/move may be subject to copy elision), then transfers control to the exception handler with the matching type whose compound statement or member initializer list was most recently entered and not exited by this thread of execution.
- throw: Rethrows the currently handled exception. Abandons the execution of the current catch block and passes control to the next matching exception handler (but not to another catch clause after the same try block: its compound-statement is considered to have been 'exited'), reusing the existing exception object: no new objects are made. This form is only allowed when an exception is presently being handled (it calls std::terminate if used otherwise). The catch clause associated with a function-try-block must exit via rethrowing if used on a constructor.
So to underline my answer, throw should be fine in your case.
ReferenceURL : https://stackoverflow.com/questions/26883615/using-throw-on-a-modified-exception
'programing tip' 카테고리의 다른 글
값을 통화로 설정 (0) | 2020.12.25 |
---|---|
Docker 이미지 내의 사용자를 루트가 아닌 사용자로 전환 (0) | 2020.12.25 |
itertools 모듈에서 izip을 가져 오면 Python 3.x에서 NameError가 발생합니다. (0) | 2020.12.25 |
`ng serve` 출력 파일은 어디에 있습니까? (0) | 2020.12.25 |
WinDbg와 VS (Visual Studio) 디버거를 사용하는 이유는 무엇입니까? (0) | 2020.12.25 |