簡體   English   中英

此方法引發兩個不同的異常。 man 如何捕獲兩個異常並將兩種異常類型捕獲的異常打印到控制台?

[英]This method throws two different exceptions. How could man catch the two exceptions and prints exception catched for both exception types to console?

public class CatchingExceptions {
    private int erroneousMethod(int p) {
        if (p == 0) {
            throw new IllegalArgumentException();
        }
        int x = 0x01;
        return p / (x >> Math.abs(p)); // this line will throw!
    }

任務是實現以下方法來捕獲並打印兩個異常。

public void catchExceptions(int passthrough) {

        erroneousMethod(passthrough); // will throw!
        try{
          ????
        } catch (IllegalArgumentException e){
            System.out.println("???? ");
        }
}

try塊中調用方法:

public void catchExceptions(int passthrough) {
    try{
        erroneousMethod(passthrough);
    } catch (RuntimeException e) { // catches all unchecked exceptions
        String message = e.getMessage() == null ? "" : (": " + e.getMessage());
        System.out.println(e.getClass().getSimpleName() + ": " + message);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM