简体   繁体   中英

Throwing and catching multiple Exceptions

Hey StackOverflow Community,

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?

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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