0

Hi I am working on a project. I am overriding a method in about 100 classes in the project, and I am using a map to populate some keys and values and returning the map. Now the map.put() method throws 4 exceptions: Null, UnsupportedOperation, IllegalArgument and ClassCast Exceptions. So should I handle all of them or not. Obviously I should, is it all?.

Then the question is should I handle these in every method overridden(100 classes) or just add throws keyword in the method and handle where I am calling these methods?

What is better practice? Its a very crucial project and no exception shouldn't go unhandled, otherwise it may crash many other things. Tell me viewing the real life scenaiors.

user2696258
  • 1,159
  • 2
  • 14
  • 26

5 Answers5

2

In general, you should catch an exception where you can add some value. Catching an exception where you don't actually know what to do with it isn't useful and likely to only obscure the problem.

As Map defines the behaviour you should support, a checked Exception is not an option.

Since most of the error are coding errors, I would handle them at a high level as you cannot easily take corrective action at runtime, i.e. the fix is to fix the code so this doesn't happen.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

First, you are not able to modify throw clause when overriding method in Java, and all the four exceptions are RuntimeException that needs no throw clause.

Second, it is important to understand when an exception will be thrown before considering how to handle with it. And some exceptions that would never happen no matter what the input is should be ignored. So, the answer depends on what implementation of Map you are using in your code.

If you are using HashMap, none of the four exceptions will ever be thrown so you can just ignore them.

And if you are using TreeMap, NullPointerException will happen if you put null value as key, and ClassCastException will happen when your key is not an instance of Comparable<T> that is currently used in keys already in the map. And you should determine weather some certain input of the overriding method will cause these two exception. If so, handle them by throw an custom exception like other answers said is preferred. Otherwise you can ignore them as well.

  • i am using linkedhashmap so can i skip? the purpose im using this one is i want the order of adding not to be changed so what do u suggest? – user2696258 Aug 21 '15 at 07:58
  • @user2696258 Yes, it inherit the `put` method from `HashMap` so no Exception is thrown. – Ruimin Wang Aug 21 '15 at 08:30
0

It would be better to write a custom exception and throw that. Do set proper message while catching it. So ultimately there will be single exception that your method needs to throw.

SacJn
  • 777
  • 1
  • 6
  • 16
0

One good practive is Throw early, catch late.

Give a look at this question

Community
  • 1
  • 1
Advicer
  • 1,300
  • 1
  • 14
  • 26
0

Well, if all of these methods are being called from the same point (or group of points), it's an obvious solution to propagate the exceptions thrown from them (add throws at the method declaration) and then handle those exceptions in the calling method. These way, you've got a single point to manage your exception handling case.
Also, all of the thrown exception do have a specific meaning (just like you said you wanna handle those you get from map.put()), please consider to use a custom exception. Here you've got some guidelines: Guidelines

Community
  • 1
  • 1
Endrik
  • 2,238
  • 3
  • 19
  • 33