예외 (C++, Java, Javascript, Python, PHP 등 ...)를 본 언어에서 catch
의 범위를 표시하기 위해 항상 try
또는 이와 비슷한 것을 볼 수 있습니다. 그것이 필요한지 궁금합니다. try
블록이없는 설계 문제는 무엇입니까?언어 디자인 (예외) : 왜 '시도'할까요?
예를 들어,이 걸릴 : 나는 대안으로이 상상
try{
try{
do_something_dangerous0();
}catch (SomeProblem p){
handle(p);
}
try{
do_something_dangerous1();
}catch (SomeProblem p){
handle(p);
}
}catch (SomeOtherProblem p){
handle(p);
}
.
do_something_dangerous0();
catch (SomeProblem p){
handle(p);
}
do_something_dangerous1();
catch (SomeProblem p){
//catches from only the second unless the first also threw
handle(p);
}
catch (SomeOtherProblem p){
//catches from either, because no other block up there would
handle(p);
}
는 "너무 많은"잡기 블록을 피하려면 새 범위를 만들 수 있습니다
do_something_dangerous2();
{
do_something_dangerous3();
catch (SomeProblem p){
//does not catch from do_something_dangerous2()
//because if that throws, it won't reach in here
handle(p);
}
}
catch (SomeProblem p){
handle(p);
}
catch (SomeOtherProblem p){
handle(p);
}
(내이 C 같은 언어에 대해 작동하지 않습니다 이유에 대한 대답 ++ 및 Java를 적어도 아래 답변에 게시되어 있지만 동적 언어에 대한 대답이 없습니다.)
일반적으로 예외 처리 범위의 시작과 끝을 정확하게 구분해야합니다. "고독한"{}'범위를 사용할 수는 있지만 실제로 아무 것도 변경하지 않으며 덜 명확합니다. –
(BEGIN/NIGEB 매크로를 사용하여 예외 처리 범위를 구분하는 시스템에서 한 번 작업했지만 전체 구조는 try/catch/finally와 사실상 동일합니다.이 함수는 구문을 결정하지만 다른 방법은 아닙니다.) –
'블록 잡기'를 피하기를 원한다면 '너무 많이'... '로 해결하지 않았습니까? – leewz