Throwing the AppException
Jakob Jenkov |
In this text I will show you how to use the AppException
shown in the text
An Exception Class Template.
The diagram below is a subsection of the diagram shown in the Overview text. It shows what happens before and until an application specific exception is thrown.
Throwing AppException's. |
In this text I try to show you how to do this in code.
Throwing an AppException
Here is a method that is supposed to load a file. It gets the file path to load as a parameter,
and throws an AppException
if the file path is null:
public byte[] loadFile(String filePath) throws AppException { // Error Detection if(filePath == null){ AppException exception = new AppException(); ErrorInfo info = exception.addInfo(); // Error Information Gathering info.setErrorId("FilePathNull"); info.setContextId("FileLoader"); info.setErrorType(ErrorInfo.ERROR_TYPE_CLIENT); info.setSeverity(ErrorInfo.SEVERITY_ERROR); info.setErrorDescription("The file path of file to load was null"); info.setErrorCorrection("Make sure filePath parameter is not null."); // Throw exception throw exception; } ... }
Notice how this example has both error detection (the if-statement), information gathering and exception throwing.
No parameter values are included in ErrorInfo
object in the example
above. It did not make sense, since we know the value of the parameter was null (filePath).
In the next example the filePath
is included in the ErrorInfo
because
now it makes sense. The example checks whether the file exists or not, and then it is relevant
to know the concrete file path.
if(! new File(filePath).exists()){ AppException exception = new AppException(); ErrorInfo info = exception.addInfo(); info.setErrorId("FileNotFound"); info.setContextId("FileLoader"); info.setErrorType(ErrorInfo.ERROR_TYPE_CLIENT); info.setSeverity(ErrorInfo.SEVERITY_ERROR); info.setErrorDescription("No file found at file path " + filePath); info.setErrorCorrection("FilePath should point to existing file."); info.getParameters().put("filePath", filePath); throw exception; }
Wrapping Alien Exceptions in an AppException
In case an exception is caught, an alien exception that is, you can wrap it in an AppException
like this:
try{ FileInputStream fileInputStream = new FileInputStream(filePath); // ... } catch (IOException e) { AppException exception = new AppException(); ErrorInfo info = exception.addInfo(); info.setCause(e); info.setErrorId("FileReadFound"); info.setContextId("FileLoader"); info.setErrorType(ErrorInfo.ERROR_TYPE_INTERNAL); info.setSeverity(ErrorInfo.SEVERITY_ERROR); info.setErrorDescription("Error processing file: " + filePath); throw exception; }
An ErrorInfo Factory
In the examples above in this text, the creation of the ErrorInfo
objects take up a lot of
lines. This may potentially clutter your code. It gets the harder to read, when so many lines are just
error handling code.
To fix this, you can move all the ErrorInfo
code into an ErrorInfoFactory
.
Here is an example:
try{ FileInputStream fileInputStream = new FileInputStream(filePath); // ... } catch (IOException e) { AppException exception = new AppException(); exception.addInfo( ErrorInfoFactory.getFileReadErrorInfo(e, filePath) ); throw exception; }
Here is the ErrorInfoFactory
method:
public class ErrorInfoFactory { public static final ErrorInfo getFileReadErrorInfo( IOException e, String filePath) { ErrorInfo info = new ErrorInfo(); info.setCause(e); info.setErrorId("FileReadFound"); info.setContextId("FileLoader"); info.setErrorType(ErrorInfo.ERROR_TYPE_INTERNAL); info.setSeverity(ErrorInfo.SEVERITY_ERROR); info.setErrorDescription("Error processing file" + filePath); return info; } }
In the next text I will show you how to propagate AppException
's up the call stack, and add information
to it on the way up.
Tweet | |
Jakob Jenkov |