欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java Exceptions

程序员文章站 2023-11-10 22:20:16
1. 常见错误分类 一般的,errors可以分为以下几类: user input errors device errors or physical limitations code errors 2. 常用错误处理方式 2.1 error code 一种常用的错误处理方法是返回error code, ......

1. 常见错误分类

一般的,errors可以分为以下几类:

  • user input errors
  • device errors or physical limitations
  • code errors

2. 常用错误处理方式

2.1 error code

一种常用的错误处理方法是返回error code,由calling method根据error code做不同处理。

但是有些情形下error code并不方便使用,比如如何取分错误码和与错误码相同的有效值。

2.2 exception handling

the mission of exception handling is to transfer control from where the error occured to an error handler that can deal with the situation.

当出现错误时,程序应当恢复到某个安全状态并重新来过,或者保存当前工作安全退出,但是这并不容易,关键就是程序控制权如何从触发错误的地方跳转到处理错误的地方。

java allows every method an alternative exit path if it is unable to complete its task in the normal way:

  • 首先,throws an object that encapsulates the error information
  • 然后,the exception-handling mechanism begins its search for an exception handler that can deal with this particular error condition
  • 注意,这条alternative exit path与正常的程序逻辑无关,the method exits immediately, and it does not return its normal value, and the execution does not resume at the code that called the method

3. java exception hierarchy

3.1 throwable

throwable是整个java exception hierarchy的基类,其下分为:

  • error
  • exception

3.2 error

the error hierarchy describes internal errors and resource exhaustion situations inside the java runtime system.

you should not throw an object of this type. there is little you can do if sucn an internal error occurs, beyond notifying the user and trying to terminate the program gracefully.

3.3 exception

exception可以分为两类:

  • runtimeexception
  • 其他

runtimeexception意味着编码错误,比如
a bad cast, an out-of-bounds array access, a null pointer access等。

其他exception一般是出现了某种意外,some bad things happened, 比如,打开一个不存在的文件。

为什么打开不存在的文件不是runtimeexception?因为这不是你的代码能控制的,你先校验文件是否存在也没用,可能你校验时是存在的,但你打开时就不存在了。

4. checked exception vs unchecked exception

error和runtimeexception这两支,我们称为unchecked exception.

除error和runtimeexception这两支外的其他exception,我们称为checked exception.

the compiler checks that you provide exception handlers for all checked exceptions:

  • 对于error,你无能为力,所以是unchecked exception
  • 对于runtimeexception,编码错误是你的责任,你应当避免它们出现,所以也是unchecked exception
  • 对于其他exceptions,你必须做好处理它们的准备,所以是checked exception

5. checked exception declaration

对于你的方法可能抛出的checked exceptions,你必须在method declaration中通过throws声明出来。

如果可能抛出多个checked exceptions,那么需要都列出来,使用逗号分隔。

public image loadimage(string name) throws filenotfoundexception, eofexception {...}

注意,unchecked exception不应当出现在你的throws声明中:

  • 对于error,你无能为力
  • 对于runtimeexception,你应当避免它们出现,而不是声明可能抛出它们。

6. throw an exception

如何抛出exception呢?很简单:

  • find an appropriate exception class
  • make an object of that class
  • throw it

没有合适的standard exception class可用?没有关系,你可以自定义一个

class fileformatexception extends ioexception {
    public fileformatexception() {}
    public fileformatexception(string gripe) {
        super(gripe);
    }
}

一般的,我们为自定义的exception class提供两个constructors:a default constructor and a constructor with a detailed message.

7. catch exceptions

try {
    xxx
} catch (exceptiontype1 | exceptiontype 2 e) {
    xxx
} catch (exceptiontype3 e) {
    xxx
}

对于checked exceptions,如果你知道如何处理它们,那么你可以catch它们,这样就不用抛出它们了。

as a general rule, you should catch those exceptions that you know how to handle and propagate those that you do not know how to handle.

8. rethrow exceptions

try {
    xxx
} catch (sqlexception e) {
    throw new servletexception("xxx error").initcause(e);
}

这是rethrow exception的常用方式。通过这种方式,我们可以包装出一个更抽象的exception,或者把一个checked exception转换成一个runtimeexception.

9. finally

try {
    try {
        xxx
    } finally {
        xxx
    }
} catch (exception e) {
    xxx
}

the inner try block has a single responsibility: to make sure that the resources are released

the outer try block has a single responsibility: to ensure that errors are reported.

注意,the body of the finally clause is intended for cleaning up resources. don't put statements that change the control flow (return, throw, break, continue) inside a finally clause.

10. try-with-resources

try (resource res = xxx) {
    xxx
}

从java 7开始,try-finally结构可以简化为try-with-resources.

要求resource必须是autocloseable的实现类,when the try block exits, then res.close() is called automatically.

a difficulty arises when the try block throws an exception and the close method also throws an exception. the original exception is rethrown, and any exceptions thrown by close methods are considered "suppressed". they are automatically caught and added to the original exception with the addsuppressed method.