Always Catch Most Specific Exception

 class​ TransmissionParser {
 static​ Transmission parse(String rawMessage) {
 if​ (rawMessage != ​null
  && rawMessage.length() != Transmission.MESSAGE_LENGTH) {
 throw​ ​new​ IllegalArgumentException(​"Bad message received!"​);
  }
 
  String rawId = rawMessage.substring(0, Transmission.ID_LENGTH);
  String rawContent = rawMessage.substring(Transmission.ID_LENGTH);
 try​ {
 int​ id = Integer.parseInt(rawId);
  String content = rawContent.trim();
 return​ ​new​ Transmission(id, content);
» } ​catch​ (Exception e) {
 throw​ ​new​ IllegalArgumentException(​"Bad message received!"​);
  }
  }
 }

Exceptions in Java are part of a relatively complex type hierarchy. When you catch an exception, you should always catch the most specific exception type. If you catch a more general type, you risk swallowing errors that you shouldn’t.

The code above parses a message of type String and dissects it into an id and a content part. The problem lies in the catch statement. Any type of Exception is caught here.

Exception is the most general exception type in Java. The only thing that’s even more general is the super type of Exception: Throwable (this comes from the catch and throw metaphor). If you catch a Throwable, you’ll even catch errors in the virtual machine, such as OutOfMemoryError. Don’t do this!

Many beginners find it tempting to catch a very general type. After all, you consume practically any type of error with a single statement.

But this is only good on the surface. It means that you catch exception types that you don’t want to handle here, such as a NullPointerException. In most cases, this exception indicates a bug in your code that you need to fix. When it occurs, you want the program to crash so that you become aware of the problem.

Just because you hide a bug by catching an exception doesn’t mean it’s fixed. It will just bring down your program at a more inconvenient point in time.

So what type of exception should you catch here?

 class​ TransmissionParser {
 static​ Transmission parse(String rawMessage) {
 if​ (rawMessage != ​null​ &&
  rawMessage.length() != Transmission.MESSAGE_LENGTH) {
 throw​ ​new​ IllegalArgumentException(​"Bad message received!"​);
  }
 
  String rawId = rawMessage.substring(0, Transmission.ID_LENGTH);
  String rawContent = rawMessage.substring(Transmission.ID_LENGTH);
 try​ {
 int​ id = Integer.parseInt(rawId);
  String content = rawContent.trim();
 return​ ​new​ Transmission(id, content);
» } ​catch​ (NumberFormatException e) {
 throw​ ​new​ IllegalArgumentException(​"Bad message received!"​);
  }
  }
 }

The fix to this problem is very simple. You just need to catch the most specific exception that might be thrown by the code in the try instead of Exception.[34]

There’s one specific type of exception that we need to take care of here: NumberFormatException. This one will be thrown by Integer.parseInt(rawId) if rawId contains something that the Integer class can’t turn into an integer value. Basically, any nondigit character will cause it.

So all you need to do is replace Exception with NumberFormatException. Then, the code will no longer swallow errors that it shouldn’t, such as a NullPointerException.

Sometimes, catching the most specific exception means that you need to catch many exceptions. This might mean that you have to write many catch blocks instead of just a single one. Don’t let the fact that more code is needed fool you into believing that catching a general exception type is better! After all, more code with less bugs is better than less code with more bugs.

If you do the same handling for all exceptions, there’s also a convenient way to get around multiple catch blocks. Since Java 7, there’s the multi-catch block. Say you want to handle a NumberFormatException and an IOException in the same way. Just write catch(NumberFormatException | IOException e) and combine two catch blocks into one. Whichever method you use to structure your catch blocks, be sure to catch the most specific exceptions only.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.22.164