Java尝试捕获块 [英] Java try catch blocks

查看:102
本文介绍了Java尝试捕获块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简单的问题:

您如何看待每个指令使用 try catch 的代码?

What do you think of code which use try catch for every instruction ?

void myfunction() {
    try {
        instruction1();
    }
    catch (ExceptionType1 e) {
        // some code
    }
    try {
        instruction2();
    }
    catch (ExceptionType2 e) {
        // some code
    }
    try {
        instruction3();
    }
    catch (ExceptionType3 e) {
        // some code
    }
    try {
        instruction4();
    }
    catch (ExceptionType4 e) {
        // some code
    }

    // etc
}

我知道这很糟糕,但我想知道这是否会降低性能。

I know that's horrible, but I want to know if that decreases performance.

推荐答案

尝试这样的事情:(我知道这不会回答你的问题,但它更清晰)

Try something like this: (I know this doesn't answer your question, but it's cleaner)

void myfunction() {
try {
    instruction1();
    instruction2();
    instruction3();
    instruction4();
}
catch (ExceptionType1 e) {
    // some code
}
catch (ExceptionType2 e) {
    // some code
}
catch (ExceptionType3 e) {
    // some code
}
catch (ExceptionType4 e) {
    // some code
}

// etc
}

这篇关于Java尝试捕获块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆