I threw an Exception xml and dynamically throws and throws an exception.
<exception-mappings> <exception-mapping key="exceptionkey1"> <class-name>com.package.CheckedException</class-name> <message>Checked Exception Message</message> </exception-mapping> <exception-mapping key="exceptionkey2"> <class-name>com.package.UnCheckedException</class-name> <message>UnChecked Exception Message</message> </exception-mapping>
I create an exception object dynamically using reflection depending on the exception key.
public static void throwException(final String key) throws CheckedException, UncheckedException { ExceptionMapping exceptionMapping = exceptionMappings.getExceptionMappings().get(key); if (exceptionMapping != null) { try { Class exceptionClass = Class.forName(exceptionMapping.getClassName()); try { throw ()exceptionClass.newInstance();
I want to know which class is for typecast on line X, so I don't need to use If / else. The reason I donโt want to use, if there is still, itโs possible that new classes may be added in the future, and I donโt want to change this code every time a new exception is added.
My basic logic is my service level, which throws either CheckedException or UncheckedException. If a CheckedException is thrown, it will be handled by my web layer. Also, I cannot select Super parent class Exception or Throwable, since my web tier is catching a CheckedException. If a UncheckedException is thrown, an exception page will be displayed.
Please help me as I cannot continue any further.
EDIT: any other decision has also been made.
java exception-handling factory-pattern
javafan
source share