Java’s Algorithm of Checking if Error is of Type (Error Inheritance Tree)


Java’s errors are usually handled via Exceptions – where you declare exceptions to throw. Then you throw as much as you can and only catch them when you know how to handle them: Throw Often, Catch Rarely.

We want to find out if an exception (or runtime error) is type of another error along its error/class inheritance Tree i.e. check if the target/expected exception type is the given exception’s parent, grandparents or some parent node up along the path.

We can use the following static method isErrorTypeOf to check if an error (Throwable) is the children of another Class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.helloacm;
 
class ErrorA extends Exception {
 
}
 
class ErrorB extends ErrorA {
 
}
 
class ErrorC extends ErrorB {
 
}
 
public class Main {
    private static boolean isErrorTypeOf(Throwable error, Class expected) {
        var err = error;
        while (err != null) {
            if (expected.isAssignableFrom(err.getClass())) {
                return true;
            }
            err = err.getCause();
        }
        return false;
    }
 
    public static void main(String[] args) {
        System.out.println(isErrorTypeOf(new RuntimeException(), ErrorA.class)); // false
        System.out.println(isErrorTypeOf(new RuntimeException(), Exception.class)); // true
        System.out.println(isErrorTypeOf(new ErrorC(), ErrorA.class));  // true
        System.out.println(isErrorTypeOf(new Exception(), ErrorA.class));  // false
    }
}
package com.helloacm;

class ErrorA extends Exception {

}

class ErrorB extends ErrorA {

}

class ErrorC extends ErrorB {

}

public class Main {
    private static boolean isErrorTypeOf(Throwable error, Class expected) {
        var err = error;
        while (err != null) {
            if (expected.isAssignableFrom(err.getClass())) {
                return true;
            }
            err = err.getCause();
        }
        return false;
    }

    public static void main(String[] args) {
        System.out.println(isErrorTypeOf(new RuntimeException(), ErrorA.class)); // false
        System.out.println(isErrorTypeOf(new RuntimeException(), Exception.class)); // true
        System.out.println(isErrorTypeOf(new ErrorC(), ErrorA.class));  // true
        System.out.println(isErrorTypeOf(new Exception(), ErrorA.class));  // false
    }
}

Here, we use the .getCause() to find the parent of the current error – until it becomes the root which doesn’t haven’t parent anymore. Then at any node, if it is Assignable From the error.getClass(), we’ve found a match and return true.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
313 words
Last Post: Teaching Kids Programming - Sudoku Validator/Algorithm using 27 Hash Sets
Next Post: Teaching Kids Programming - Combination Sum Up to Target (Unique Numbers) by Dynamic Programming Algorithms

The Permanent URL is: Java’s Algorithm of Checking if Error is of Type (Error Inheritance Tree)

Leave a Reply