Hey. Do I catch the NullPointerException correctly in this case, or are there other, shorter ways?

 try { try { numBytesRead = mPort.read(buffer, 1000); } catch (IOException e) { e.printStackTrace(); } }catch (NullPointerException e) {} 

Moreover, the mPort check for null not appropriate, because the error periodically occurs when I break the connection with my USB device, which at that moment sent the data, while mPort still exists, but an exception is thrown inside the read method.

Thank.

3 answers 3

There is no need to cascade try, you can consistently describe the handling of different exceptions:

  try { numBytesRead = mPort.read(buffer, 1000); } catch (IOException e) { e.printStackTrace(); } catch (NullPointerException e) { e.printStackTrace(); } 

There is still multicatch (thanks to Regent for the reminder) if you want to handle several types of exceptions in the same way:

  try { numBytesRead = mPort.read(buffer, 1000); } catch (IOException | NullPointerExceptione) { e.printStackTrace(); } 
  • Thank you, I did not know that there is such a construction :) - GlWhitefoot
  • 2
    @GlWhitefoot if exceptions are supposed to be handled in the same way, then not only one try enough, but also one catch : catch (IOException | NullPointerException e) { e.printStackTrace(); } catch (IOException | NullPointerException e) { e.printStackTrace(); } . - Regent
  • @Regent well, I completely forgot java. )) - Nick Volynkin
  • @Regent android does not support java 8 - Real KEK
  • @Sokolov and how is this related to the Multi-catch exceptions introduced in Java 7 ? - Regent

Do not do this:

 catch (NullPointerException e) { } 

if this is really not necessary.

If, at the occurrence of a NullPointerException nothing really should happen, then you can somehow explicitly indicate this, for example:

 catch (NullPointerException ignored) { } 

There is no need for a nested try-catch . Any exceptions can be processed in one block:

 try { numBytesRead = mPort.read(buffer, 1000); } catch (IOException e) { // handle exception } catch (NullPointerException e) { // handle exception } 

Note that in blocks with several catch , you should first handle narrower exceptions.

  • Plus for ignored . - Nick Volynkin

Um ... but can not you somehow simpler?

 try { if(mPort != null) numBytesRead = mPort.read(buffer, 1000); else Log.w(TAG, "Port is zero!"); } catch (Throwable th) { Log.e(TAG, "Something went wrong reading from port", th); } 

Why bring to NPE - try / catch is quite an expensive operation, and comparing to null is about nothing at all.

And secondly, why write printStackTrace() - you need to use integrated logging.

If you really want to catch all exceptions, you have to catch Throwable .

  • Thanks for the answer. But on this example, I tested. Sometimes at the time of pulling out the USB wire from the device, these checks do not pass, an error occurs inside the read method. - GlWhitefoot
  • And then a plus for logging) - Nick Volynkin
  • Updated the answer - then you need to catch Throwable - Barmaley