...
However, there may be a reason to catch an exception or two.
Jumping In The Way
The previous example logged retrievals even when it didn’t work. This example more accurately distinguishes successes from failures.
Code Block | ||
---|---|---|
| ||
class AssetLookupLoggingDecorator implements org.osid.repository.AssetLookupSession { private org.osid.repository.AssetLookupSession next; public org.osid.repository.Asset getAsset(org.osid.id.Id assetId) throws org.osid.NotFoundException, org.osid.OperationFailedException, org.osid.PermissionDeniedException { try { return (this.next.getAsset(assetId)); } catch (org.osid.OsidException oe) { log(getEffectiveAgent() + " could not get " + assetId + " because of " + oe.getMessage()); throw oe; } finally { log(getEffectiveAgent() + " looking up " + assetId); } } } |
We don’t want to interfere with the exception chain so this re-throws the same exception.
This could have caught each of the NotFoundException, OperationFailedException, and PermissionDeniedException explicitly, but OsidException was a bit easier. All org.osid.OsidExceptions are checked exceptions declared in the method signatures. We cannot get any other kind of checked exception from getAsset() so the broader net is fine in this case (note that org.osid.OsidRuntimeException is like java.lang.RuntimeException but org.osid.OsidException is not like java.lang.Exception because it does not include org.osid.OsidRuntimeExceptions).
What about unchecked exceptions? The specification also explicitly permits org.osid.NullArgumentException and the runtime may throw a variety of org.osid.ProviderContractExceptions and org.osid.ConsumerContractExceptions. All of these exceptions result not from the operation of the code but from some kind of issue with the code itself. There’s no point in handling errors in code that is broken. In a running application, you may wish to catch them at your last net and open a jira.
In most cases, there is no reason to catch org.osid.OsidRuntimeException of any of its subclasses.
See Also
...