0

I want to assign an error code to each error, so I think I have to find all sub classes of java.lang.Throwable in the JVM. The classes may or may not have been already loaded in the JVM. Or is there another solution?

I have read the similar question How do you find all subclasses of a given class in Java? , and tried the ClassGraph and org.reflections:reflections:0.10.2 , but they can not find the Classes in jdk like the java.lang.NullPointerException in java.lang.

The code using ClassGraph:

ClassGraph classGraph = new ClassGraph() ;
ScanResult result = classGraph.enableAllInfo().whitelistPackages( "java.lang" ).scan() ;
result.getAllClasses().forEach( c -> System.out.println(c) ) ;
Bourbon_7
  • 161
  • 7

2 Answers2

0

You can't do it easily with standard java modules. Several third partie artifacts can allow you to do it, for example ClassGraph. IF you are using a framework like Spring, there are also tools to do that in there.

Alternatively, you can maybe just use the exception class' hash code as an error code, for example Exception.class.hashCode(). There is normally no reason that this hash code change between two JVM executions. You can't completely exclude that two classes share the same hash code, but over 2^32 possibilities, the probability is still extremely low.

However, do you really need to do that ? Probably not.

You should be able to concentrate on exceptions that can really occurrs in your program in a given context, and assign an error number only on those particular cases. Maybe there would be a few dozens, maybe a few hundreds if your software is really big and complex, but probably still much less than there are exception classes in the JVM.

You would have the great benefit to group error codes as you like in a logical way, for example saying that 400-499 is client-side error and 500-599 is server-side error, as in HTTP, or 10000-19999 is network-related as in windows OS, etc. instead of relying on random values given by hash codes or another more or less automatic process.

QuentinC
  • 12,311
  • 4
  • 24
  • 37
-1

Just create a custom exception with error code and wrap any exception into your custom exception.

Enum with all codes:

public enum ErrorCodes {
    VALIDATION_PARSE_ERROR(422);

    private int code;

    ErrorCodes(int code) {
        this.code = code;
    }

    public int getCode() {
        return code;
    }
}

Create a custom exception:

public class MyCustomException extends RuntimeException {

    private Integer errorCode;

    public MyCustomException(String message) {
        super(message);
    }

    public MyCustomException(String message, Throwable cause) {
        super(message, cause);
    }

    public MyCustomException(String message, Throwable cause, ErrorCodes errorCode) {
        super(message, cause);
        this.errorCode = errorCode.getCode();
    }

    public Integer getErrorCode() {
        return errorCode;
    }
}

And throw this exception like this.

public String convertDollarsToEuros(String value) {
    try {
        int x = Integer.parseInt(value);
    } catch (NumberFormatException e) {
        throw new MyCustomException("Invalid data", e, ErrorCodes.VALIDATION_PARSE_ERROR);
    }
    return value;
}