![]() Main page FAQ HOWTO Components Page Index myWMS News ![]() Mailinglist ![]() lexicon ![]() Recent Changes WikiEtiquette Find pages Unused pages Undefined pages Impressum ![]()
Set your name in
UserPreferences Referenced by
JSPWiki v2.2.33
i18n support |
Exception Handling in Generall
Forwarding ExceptionsOften it comes to the point, where the exception you cannot handle must be forwarded to the caller of the method, but because the interface of the method does not announce the special exception it cannot be done directly. In this case:Try to wrap the exception into an announced exception:
public void myExample(int i) throws IllegalArgumentException {
try {
[...]
}
catch(ProprietaryException ex) {
throw new IllegalArgumentException(ex);
}
}
Wrap the catched exception into a RuntimeException:
public void myExample() {
try {
[...]
}
catch(ProprietaryException ex) {
throw new RuntimeException(ex);
}
}
RuntimeExceptionsRuntimeExceptions should not be catched at all. RuntimeExceptions are programmer errors.RuntimeExceptions are defined leading to an undefine state of the software. Handling Ressources(for instance Transactions)If a ressource is used, the programmer has to ensure, that the ressource is freed afterwards. If a transaction is used, the programmer has to ensure, that it is closed afterwards. Implementing this is easy within java. Please take the following snippet, which contains the basic elements of this strategy:
tid = kernelUtil.begin();
try {
[...]
}
finally {
kernelUtil.clear(tid);
}
(kernelUtil is an instance of the class EquipmentKernelUtil.) Other Links
|
||||||