java-ee - 关于Java异常捕获的问题
PHPz
PHPz 2017-04-18 09:51:10
0
7
416

如果捕获异常没有专门的处理,只是为了记录日志,那么异常究竟是这样

public void xxx() {
    try {
        ...
        ...
        ...
    } catch() {
        ...
    }
}

捕获好还是这样

public void xxx() {
    try {
        ...
    } catch() {
        ...
    }
    try {
        ...
    } catch() {
        ...
    }
    try {
        ...
    } catch() {
        ...
    }
}

捕获好,另外,是每一种异常单独捕获

public void xxx() {
    try {
        ...
        ...
        ...
    } catch(aaException e) {
        ...
    } catch(bbException e) {
        ...
    } catch(Exception e) {
        ...
    }
}

好,还是全部都用Exception捕获

public void xxx() {
    try {
        ...
        ...
        ...
    } catch(Exception e) {
        ...
    }
}

好?

PHPz
PHPz

学习是最好的投资!

reply all(7)
伊谢尔伦

The more elegant way is to handle it through Aop, so that you don’t have to write repeated try and catch in the business code

PHPzhong

It’s better to use Exception

左手右手慢动作

Just to keep a log, the last one can explain the problem.

Ty80

There is no need to subdivide the logs, printing the stack information is clear at a glance

PHPzhong

This depends on the scenario: In the third case, different exceptions are captured separately in order to handle the captured exceptions in a fine-grained manner, such as capturing cache exceptions, performing DB switching, capturing IO exceptions, and remediating them; fourth The first is to wrap an Exception and process it once, but some specific information will be lost
My understanding is that dividing into so many exception types can be considered an extension of switch-case

黄舟

This depends on the situation. If you don’t need to handle the exception, you can just replenish the exception at the low level. If you need to handle it, just catch the replenishment and handle it. For example, you can define your own prompt information for exceptions.

阿神

Just catch the exceptions you need to pay attention to. Other exceptions may be caught using Exception.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template