是的,我们可以在catch和finally块中写方法的返回语句。
public class CatchReturn { int calc() { try { int x=12/0; } catch (Exception e) { return 1; } return 10; } public static void main(String[] args) { CatchReturn cr = new CatchReturn(); System.out.println(cr.calc()); } }
1
public class FinallyReturn { int calc() { try { return 10; } catch(Exception e) { return 20; } finally { return 30; } } public static void main(String[] args) { FinallyReturn fr = new FinallyReturn(); System.out.println(fr.calc()); } }
30
以上就是在Java中,我们可以在catch或finally块中使用return语句吗?的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号