Java Try Catch块 [英] Java Try Catch block

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

问题描述

我最初在大学开始编程,学习vb.net。现在我已经决定移动到Java并有一些查询。在vb中,try catch语句的布局如下

 尝试
作为异常处理例外

结束catch

但是从java网站( https://docs.oracle.com/javase/tutorial/essential/exceptions/putItTogether.html
i发现在java中你可以使用两个catch:

  try {

} catch(ExceptionType name){

} catch(ExceptionType name){

}

我希望有人能够解释为什么你需要在java中使用两个catch,并且相应的catch捕捉到什么。



谢谢。 / p>

解决方案

在Java中,你可以使用多个 catch 块。



这不一定意味着你必须。



这取决于您在尝试块中的代码,以及多少个例外可能会抛出(甚至未选中异常如果你真的想抓住这个,通常你不会,你不会必须)。



一个不好的做法是使用单个处理程序来执行一般的异常(或更糟, Throwable ,这也将捕获 RuntimeException s和错误 s):

  try {
//抛出多个异常的东西
}
/ / bad
catch(Exception e){
// TODO
}

良好练习是捕获所有可能投掷的 异常



如果其中一些与继承有关,则始终先抓住子类(即更具体的异常 s),以免你的代码不会编译:

  try {
// throws FileNotFoundException AND IOException
}
//好:FileNotFoundException是IOException的子类 - 检查
catch(FileNotFoundException fnfe){
// TODO
}
catch (IOException ioe){
// TODO
}

另外看看在Java 7的多个catch块 ,其中不相关的异常可以在每个异常之间一个 | 类型:

  try(可选的资源){
//抛出FileNotFoundException的东西和MyOwnCheckedException
}
//下面异常是不相关的
catch(FileNotFoundException | MyOwnCheckedException e){
// TODO
}

注意



这个链接的例子,下面的第一个代码片段将它们整合在一起可能被认为是次优的:它 捕获潜在的抛出异常,但其中之一是一个 IndexOutOfBoundsException ,这是一个 RuntimeException (未经检查),理论上不应该处理。



相反, SIZE 变量(或可能的常量)应替换为对列表被迭代,即 list.size(),以防止 IndexOutOfBoundsException 被抛出。



我想在这种情况下只是提供一个例子。


I initially started programming in college and learnt vb.net. Now I have decided to make the move to Java and have some queries. In vb, the try catch statement is laid out as follows

try
Catch ex as exception
finally
End catch

but from the java website (https://docs.oracle.com/javase/tutorial/essential/exceptions/putItTogether.html) i found that in java you use two catches like so:

    try {

} catch (ExceptionType name) {

} catch (ExceptionType name) {

}

i was hoping someone could explain why you need two catches in java and what do the respective catches do/catch.

Thanks.

解决方案

In Java, you can use multiple catch blocks.

It doesn't necessarily means you have to.

It depends on the code your have in the try block, and how many checked Exceptions it may potentially throw (or even unchecked Exceptions if you really want to catch that, typically you don't and you don't have to).

One bad practice is to use a single handler for general Exception (or worse, Throwable, which would also catch RuntimeExceptions and Errors):

try {
    // stuff that throws multiple exceptions
}
// bad
catch (Exception e) {
    // TODO
}

The good practice is to catch all potentially thrown checked Exceptions.

If some of them are related in terms of inheritance, always catch the child classes first (i.e. the more specific Exceptions), lest your code won't compile:

try {
    // stuff that throws FileNotFoundException AND IOException
}
// good: FileNotFoundException is a child class of IOException - both checked
catch (FileNotFoundException fnfe) {
    // TODO
}
catch (IOException ioe) {
    // TODO
}

Also take a look at Java 7's multi-catch blocks, where unrelated Exceptions can be caught all at once with a | separator between each Exception type:

try (optionally with resources) {
    // stuff that throws FileNotFoundException and MyOwnCheckedException
}
// below exceptions are unrelated
catch (FileNotFoundException | MyOwnCheckedException e) {
    // TODO
}

Note

In this example you linked to, the first code snippet below Putting it all together may arguably be considered as sub-optimal: it does catch the potentially thrown Exceptions, but one of them is an IndexOutOfBoundsException, which is a RuntimeException (unchecked) and should not be handled in theory.

Instead, the SIZE variable (or likely constant) should be replaced by a reference to the size of the List being iterated, i.e. list.size(), in order to prevent IndexOutOfBoundsException from being thrown.

I guess in this case it's just to provide an example though.

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

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