I want to have a single abstract class InitException
to represent a set of classes, ie StartParameterException
, FileNotFoundException
, etc. I define StartParameterException
myself, but FileNotFoundException
is a standard exception that I import. So I can not add extends InitException
to its definition, as I do for my own classes:
public class StartParameterException extends InitException {
...
}
The main point is to change this:
try {
...
} catch (StartParameterException | FileNotFoundException | InvalidPathException | NullBoardException e) {
System.out.println(e);
System.exit(INIT_ERROR);
}
to this
try {
...
} catch (InitException e) {
System.out.println(e);
System.exit(InitException.ERROR_CODE);
}
Is this possible? Should I use an interface somehow?
You may try to create a set of exception classes. And then check whether the caught exception is within the set
Set<Class<?>> setClassesInitExc = new HashSet<>();
setClassesInitExc.add(FileNotFoundException.class);
setClassesInitExc.add(InvalidPathException.class);
setClassesInitExc.add(StartParameterException.class);
setClassesInitExc.add(NullBoardException.class);
try {
...
} catch(Exception e) {
if (setClassesInitExc.contains(e.getClass())) {
System.out.println(e);
System.exit(InitException.ERROR_CODE);
}
}
Another way is to create an enum
with supported exception types and add some context required to handle each exception, something like this:
public enum ExceptionEnum {
START_PARAMETER_EXCEPTION(StartParameterException.class, "Start parameter exception occurred", 1),
FILE_NOT_FOUND_EXCEPTION(FileNotFoundException.class, "Required file is not found", 2),
INVALID_PATH_EXCEPTION(InvalidPathException.class, "Given path is not valid", 3),
NULL_BOARD_EXCEPTION(NullBoardException.class, "Null board exception occurred", 4);
private Class exceptionClass;
private String errorMessage;
private int exitCode;
public Class getExceptionClass() {
return exceptionClass;
}
public String getErrorMessage() {
return errorMessage;
}
public int getExitCode() {
return exitCode;
}
ExceptionEnum(Class exceptionClass, String errorMessage, int exitCode) {
this.exceptionClass = exceptionClass;
this.errorMessage = errorMessage;
this.exitCode = exitCode;
}
public static ExceptionEnum fromExceptionClass(Class clazz) {
for (ExceptionEnum exceptionEnum : ExceptionEnum.values()) {
if (exceptionEnum.getExceptionClass().isAssignableFrom(clazz)) {
return exceptionEnum;
}
}
return null;
}
}
More context to each exception can be added and this provides more control on how individual exceptions can be handled at run time:
try {
//....
} catch (Exception e) {
ExceptionEnum exceptionEnum = ExceptionEnum.fromExceptionClass(e.getClass());
if(exceptionEnum != null) {
System.out.println(exceptionEnum.getErrorMessage());
System.exit(exceptionEnum.getExitCode());
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.