In those languages ββwhere I saw exceptions (C ++, Java, Javascript, Python, PHP, ...), I always see trysomething like that to highlight the area catch. I wonder if this is necessary. What are the design issues when NOT have tryblocks?
For example, take this:
try{
try{
do_something_dangerous0();
}catch (SomeProblem p){
handle(p);
}
try{
do_something_dangerous1();
}catch (SomeProblem p){
handle(p);
}
}catch (SomeOtherProblem p){
handle(p);
}
I present this as an alternative.
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);
}
If you want to avoid blocking too much, you can create a new area:
do_something_dangerous2();
{
do_something_dangerous3();
catch (SomeProblem p){
handle(p);
}
}
catch (SomeProblem p){
handle(p);
}
catch (SomeOtherProblem p){
handle(p);
}
(My answer is why this will not work for languages ββlike C ++ and Java, at least posted as the answer below, but I have no answer for dynamic languages.)
source
share