user1883212
user1883212

Reputation: 7869

An exception that cannot be rethrown, overridden, dealt with or wathever

I have a servlet that call this method

TemplateLoader.load(TemplateReplacer replacer);

The particular implementation of TemplateReplacer can generate a MalformedURLException, because it replaces URLs.

My dilemma is:

  1. I cannot leave this exception to go up to the TemplateLoader level, because TemplateLoader is generic and shouldn't catch or throw a MalformedURLException
  2. I cannot deal with the exception in TemplateReplacer, because there is nothing I can do at this level.
  3. I cannot rethrow a RuntimeException because the right things to do is catch the exception at the servlet level, where I can deal with it by returning the right error message to the user.
  4. However I cannot find an existing checked exception I can reuse, generic enough to make sense at TemplateLoader level.
  5. And I cannot use the exception Exception, because it doesn't sound good to me to just catch the exception Exception at servlet level. Just catching Exception is considered bad practice.
  6. I also cannot create my own exception, because there is no added value (as there is no real new code I can add to this custom exception)

So, I'm stuck in this dilemma. What do you suggest me to do?

Upvotes: 0

Views: 60

Answers (1)

Rob
Rob

Reputation: 6497

The solution to your problem is to define and use a custom exception. Your analysis in your #6 is flawed. The value that the custom exception provides is to group together (encapsulate) the various exceptions that might occur in the concrete implementations.

You may want to create more than one custom exception. For example, one to indicate a transient problem (and a retry might work) and one to indicate a fundamental problem with the configuration and that something needs to change before success is possible. If you create multiple exceptions, you probably should consider an inheritance hierarchy, so a caller can deal with the generic exception or with the specific flavors of the exception as appropriate.

Upvotes: 1

Related Questions