Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Less try / More catch

I need to know if doing less Try and doing more Catchs is a good way to control a flux what is very important in exception control terms.

Because in the flux this should never go on if there's something goes wrong!

i'm not trying to save code lines, i need something visually easy to understand and functionally in code terms

Var ix;
Var iy;
Var iz;

try {
    try {
        do something ix;
    } catch (Exception ex1) {
        ex1.printStackTrace();
    }

    try {
        do something iy;
    } catch (Exception ex2) {
        ex2.printStackTrace();
    }

    do something iz;
} catch (Exception ex3) {
    ex3.printStackTrace();
}

OR

Var ix;
Var iy;
Var iz;

try {
        do something ix;
        do something iy;
        do something iz;

} catch (Exception ex1) {
    ex1.printStackTrace();
} catch (Exception ex2) {
    ex2.printStackTrace();
} catch (Exception ex3) {
    ex3.printStackTrace();
}
like image 863
user2769284 Avatar asked Mar 11 '26 03:03

user2769284


1 Answers

Those two examples will actually behave differently. In the second, if an exception is caught then none of the following do something will run. In the first they can each fail independently.

like image 108
telkins Avatar answered Mar 12 '26 18:03

telkins



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!