【问题标题】:Throwing and catching multiple Exceptions抛出和捕获多个异常
【发布时间】:2018-10-27 09:53:42
【问题描述】:

嘿 StackOverflow 社区,

我正在尝试编写可以抛出和捕获多个异常的代码。 可能是什么问题?

我想得到这个输出:

Doing risky
Boi
Fooi
Fooi
Fooi
FINAAAL WIN

主类如下所示:

public class Dorisk {

public static void main(String[] args) {

    Dorisk dora = new Dorisk();

    try {
        dora.Dorisky(1);
    }catch(BoinkException bo){
        System.out.println("Boi");
    }catch(FooException fo){
        System.out.println("Fooi");
    }catch(BazException ba){
        System.out.println("Baaai");
    }finally{
        System.out.println("FINAAAL WIN");
    }
}



public void Dorisky(int x)throws BazException{

        while( x < 5 ){
        System.out.println("Doing risky");
        if(x ==1){
        throw new BoinkException();
        }
        if(x ==2){
        throw new BiffException();
        }   
        if(x ==3){
        throw new BarException();
        }
        if(x ==4){
        throw new FooException();
        }
    x++;

    }
  }
}

例外情况是:

public class BazException extends Exception{

    public BazException(){
        System.out.println("Baz baja");
    }
}

public class FooException extends BazException{

    public FooException(){
        System.out.println("Foo baja");
    }
}

public class BarException extends FooException{

    public BarException(){
        System.out.println("Bar baja");
    }
}

public class BiffException extends FooException{

    public BiffException(){
        System.out.println("Biff baja");
    }
}


public class BoinkException extends BiffException{

    public BoinkException(){
        System.out.println("Boink baja");
    }
}

但我得到的是:

Doing risky
Baz baja
Foo baja
Biff baja
Boink baja
Boi
FINAAAL WIN

什么告诉我只有 doRisky 方法中的第一个异常被抛出,但是为什么?

感谢您的回答!

编辑:我现在明白了!第一个抛出的 Exception 打印了所有其他消息,因为它们是在 Exception 超类的构造函数中声明的,并且必须构造它们,这样子类才能运行。

【问题讨论】:

  • 因为方法执行在 throw new BoinkException() 处停止。以下 if 不会被执行。
  • 那为什么其他异常会被执行呢?
  • 因为当你抛出异常时,异常之后的语句不会被执行!
  • throwreturn 类似,只是它一直被抛出调用方法,直到有人抓住它。

标签: java exception try-catch


【解决方案1】:

您的Dorisky 方法在 x = 1 时抛出异常, 表示Dorisky 方法返回调用者方法,BoinkException 异常。

if(x ==1){
    throw new BoinkException();
}

首先,为什么要返回多个异常?

这不是正确的设计方式。顺便说一句...为了您的理解,我实施了。

在这里,我为每个 throw 和 ExceptionList 创建了 CustomException,其中包含可抛出的异常列表。

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    private static ArrayList<Exception> ex = new ArrayList<Exception>();

    private static class CustomException extends Exception {
        int i;
        public CustomException(int i) {
            this.i = i;
        }

        public String toString() {
            return "Exception: " + i;
        }
    }

    private static class ExceptionList extends Exception {
        ArrayList<Exception> ex = new ArrayList<Exception>();
        public ExceptionList(ArrayList<Exception> ex) {
            this.ex = ex;
        }

        public ArrayList<Exception> getEx() {
            return ex;
        }
    }

    public static List<Exception> process() throws Exception {
        int i = 0;
        while(i < 5) {
            if(i == 1) {
                ex.add (new CustomException(i));
            } else if(i==2) {
                ex.add (new CustomException(i));
            } else if(i==3) {
                ex.add (new CustomException(i));
            }
            i++;
        }

        if(ex.size() > 0) {
            throw new ExceptionList(ex);
        } else {
            return null;
        }
    }

    public static void main (String[] args) throws java.lang.Exception
    {
        try {
            new Ideone().process();
        } catch(ExceptionList ex) {
            for(Exception ei : ex.getEx()) {
                System.out.println(ei.toString());
            }
        }
    }
}

Output 

Exception: 1
Exception: 2
Exception: 3

【讨论】:

    猜你喜欢
    • 2013-06-24
    • 2018-03-09
    • 2010-10-03
    • 1970-01-01
    • 1970-01-01
    • 2016-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多