简体   繁体   English

抛出并捕获多个异常

[英]Throwing and catching multiple Exceptions

Hey StackOverflow Community, 嘿StackOverflow社区,

I am trying to write code that throws and catches multiple Exceptions that I made. 我想写一个抛出和捕捉,我由多种异常的代码。 What might be the problem? 可能是什么问题?

I want to get this output: 我想得到这个输出:

Doing risky
Boi
Fooi
Fooi
Fooi
FINAAAL WIN

The main class looks like this: 主类如下所示:

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++;

    }
  }
}

And the Exceptions are : 而唯一的例外是:

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");
    }
}

BUT what I get is: 但是我得到的是:

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

What tells me that only the first Exception in the doRisky method gets thrown, but why? 什么告诉我,只有在doRisky方法的第一个例外得到投掷,但为什么呢?

Thank you for the answers! 感谢您的回答!

Edit: I got it now! 编辑:我明白了! The first thrown Exception printed all the other messages, because they were declared in the constructor of the Exception superclasses, and they have to be constructed, so the subclass can run. 第一抛出的异常印刷的所有其它消息,因为它们是在异常超类的构造函数中声明,他们必须被构造,所以子类可以运行。

Your Dorisky Method throws the exception when x = 1, means Dorisky method return with BoinkException exception to caller method. 当x = 1时,您的Dorisky方法将引发异常,这意味着Dorisky方法将BoinkException异常返回给调用方方法。

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

First, Why you want to return multiple exceptions? 首先,为什么要返回多个异常?

It is not the right way to design. 这不是正确的设计方法。 BTW... I implemented for your understanding. 顺便说一句...我为您的理解而实施。

Here, I created CustomException for each throw and ExceptionList that holds the list of throwable exception. 在这里,我为每个throw和ExceptionList创建了CustomException,该ExceptionList保存了可抛出异常的列表。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM