I am creating my own Exception class for a simple command line tool I created. Basically the purpose is to decide when a fatal Exception is thrown and to terminate the program in that case. So my ReconToolException class has a boolean flag to record a fatal Exception.
However, my question is, what is the proper way to "throw" the Exception?
Should I do it this way?:
if(columnNumberForGroupByColumnFile1 == null || columnNumberForGroupByColumnFile2 == null ||
columnNumberForCountColumnFile1 == null || columnNumberForCountColumnFile2 == null ){
new ReconToolException("Can't find column number. Likely that there are unexpected headers in the CSV files. Check your CSV configuration enum .java files to see if they match the actual input CSV files.");
}
or should I do it the following way?:
if(columnNumberForGroupByColumnFile1 == null || columnNumberForGroupByColumnFile2 == null ||
columnNumberForCountColumnFile1 == null || columnNumberForCountColumnFile2 == null ){
try {
throw new ReconToolException("Can't find column number. Likely that there are unexpected headers in the CSV files. Check your CSV configuration enum .java files to see if they match the actual input CSV files.");
} catch (ReconToolException e) {
e.printStackTrace();
}
}
the former seems to require that I explicitly code a print out statement in my ReconToolException class to print the error to the command line. The latter will catch itself (which is very strange AND honestly seems incorrect) and will print out the stack trace in the catch block. The former seems to make more sense, but the latter seems to work better. Is there a correct way?
here is the Exception class I created:
public class ReconToolException extends Exception {
private boolean isFatal;
public ReconToolException() {
// TODO Auto-generated constructor stub
}
public ReconToolException(boolean isFatalException, String msg) {
super(msg);
this.setFatal(isFatalException);
}
public ReconToolException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public ReconToolException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
public ReconToolException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public ReconToolException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
public boolean isFatal() {
return isFatal;
}
public void setFatal(boolean isFatal) {
this.isFatal = isFatal;
}
}
RuntimeException.