Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throwing a Linked List of Exceptions in Java

Tags:

I have a function that loops while doing something that could throw an exception. Looks something like this:

public void myFunction() throws MyException {
    while(stuff) {
        try {
            DoSomething() // throws an exception
        }
        catch (Exception ex) {
            throw new MyException(some, stuff, of, mine, ex);
        }
    }
}

The error causing the exception is recoverable. It can be something like an SQL error in a single update statement where the while loop executes a series of update statements. Or a parsing error in a single piece of data, where the loop is processing multiple pieces of data. I need to pass the exception further up the chain so the GUI part of the program can process it, handle it and pass on the error to the user. But I don't want to kill the loop in this particular function. The other things it's doing might not be invalid. The error that caused the exception might not be fatal to the function.

So my question is this: Is it an acceptable practice to build linked lists of custom exceptions (where each exception is a node, and the exception thrown is the head of the list) and then throw the head of the list (if any) once the loop finishes?

Has anyone ever seen this done? Can anyone think of any potential problems with doing this? Can anyone think of other, better ways to handle the root problem: the need to pass up multiple unrelated exceptions with out exiting the function until it is done?

Here's an example of how the linking and throw might be implemented very simply:

public void myFunction() throws MyException {
    MyException head = null;
    while(stuff) {
        try {
            DoSomething() // throws an exception
        }
        catch (Exception ex) {
            MyException tmp = new MyException(some, stuff, of, mine, ex);
            tmp.next(head);
            head = tmp;
        }
    }
    if(head != null)
       throw head;
}
like image 539
Daniel Bingham Avatar asked Feb 04 '10 20:02

Daniel Bingham


People also ask

How do I throw an exception list?

Using the Throws keyword Throws is a keyword used to indicate that this method could throw this type of exception. The caller has to handle the exception using a try-catch block or propagate the exception. We can throw either checked or unchecked exceptions.

Can a method throw multiple exceptions in Java?

A method can throw one of several exceptions. Eg: public void dosomething() throws IOException, AWTException { // .... } This signals that the method can eventually throw one of those two exceptions (and also any of the unchecked exceptions).

What is linked exception in Java?

A linked exception is implemented by using the chained exception mechanism of the java. lang. Throwable class, and an application obtains a linked exception by calling the Throwable. getCause() method. From an exception that is an instance of MQException, MQException.

What exceptions can you throw in Java?

The throw keyword in Java is used to explicitly throw an exception from a method or any block of code. We can throw either checked or unchecked exception. The throw keyword is mainly used to throw custom exceptions.


2 Answers

My intial thought (other than I've not seen this) is that an exception is potentially quite a large object (containing the stacktrace) and I'd prefer not to store a lot of them.

I would instead build a list of the erroneous parameters/arguments as exceptions occur, and upon completion of the loop, throw a custom exception populated with this list (if the list has more than 0 elements). That would seem a more manageable way of handling this scenario.

public void myFunction() throws CustomException {
    List<MyError> errors = new ArrayList<MyError>();
    while(stuff) {
        try {
            DoSomething() // throws an exception
        }
        catch (Exception ex) {
            errors.add(new MyError(some, stuff, of, mine, ex));
        }
    }
    if (errors.size() > 0) {
       throw new CustomException(errors);
    }
}
like image 164
Brian Agnew Avatar answered Sep 20 '22 02:09

Brian Agnew


Do you really need to throw all the exceptions? How do you expect to the individual, unrelated exceptions to be handled? Generally in cases like this, the system will just report the errors and be done with it.

If so, you might want to just collect the error messages and add them to a custom Exception class and throw that.

like image 45
Kevin Avatar answered Sep 23 '22 02:09

Kevin