java中try-catch和throw之间的区别 [英] Difference between try-catch and throw in java

查看:542
本文介绍了java中try-catch和throw之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

try-catch和throw子句有什么区别。何时使用这些?

What is the difference between try-catch and throw clause. When to use these?

请告诉我。

推荐答案


  • 尝试块将执行一个可以抛出异常的敏感代码

  • 只要在try块中抛出异常(catch类型),就会使用catch 块。

  • finally 每个情况下调用$ c> block。即使未捕获异常或前一个块中断执行流程。

  • throw 关键字将允许您抛出异常(会破坏执行流程,可以在 catch 块中捕获)。

  • 方法原型中的throws 关键字用于指定您的方法可能抛出指定类型的异常。当您检查了当前方法中不想捕获的异常(您必须处理的异常)时,它非常有用。

    • The try block will execute a sensitive code which can throw exceptions
    • The catch block will be used whenever an exception (of the type caught) is thrown in the try block
    • The finally block is called in every case after the try/catch blocks. Even if the exception isn't caught or if your previous blocks break the execution flow.
    • The throw keyword will allow you to throw an exception (which will break the execution flow and can be caught in a catch block).
    • The throws keyword in the method prototype is used to specify that your method might throw exceptions of the specified type. It's useful when you have checked exception (exception that you have to handle) that you don't want to catch in your current method.
    • 资源:

      • oracle.com - Lesson: Exceptions

      另一方面,你应该接受一些答案。如果有人遇到与您相同的问题并找到您的问题,他/她将很乐意直接看到问题的正确答案。

      On another note, you should really accept some answers. If anyone encounter the same problems as you and find your questions, he/she will be happy to directly see the right answer to the question.

      这篇关于java中try-catch和throw之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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