I have a list of exceptions (with package name path: com.a.b.ExceptionA
) and error codes map.
Now, user will call new ApiException(errorcode,"message")
, or new ApiException(errorcode,"message", Throwable t)
. Then, inside BaseException
, I want to create ExceptionA,B based upon errorcode and throw it.
I can look up the map and get the exception type path, but how to create the exception and throw it? So some thing like below? or any better approach?
Class<?> c = Class.forName("com.a.b.ExceptionA");
Constructor<?> cons = c.getConstructor(String.class);
Object object = cons.newInstance("message");
throw object;
Thanks!