Question from the test. (my option is bold) Can one catch block deal with handling and exceptions like NullPointerException, and IOException?

  1. Yes
  2. Not

1 answer 1

To solve such questions, you should have a test class and a little desire to figure it out yourself:

import java.io.IOException; public class ExceptionTest { public static void main(String[] args) { try { nullPointerException(); iOException(); } catch (Exception ex) { System.out.println(ex.getClass().getName()); } } private static void nullPointerException() { throw new NullPointerException(); } private static void iOException() throws IOException { throw new IOException(); } } 

I will push you a little ...

  • Well, this is not correct. Your block is not only concerned with these two exceptions. Of course, this is clearly not spelled out in the question, but I understand that this was expected: catch (NullPointerException | IOException ex) But the answer is really easy to google, checked on request "several exceptions in one catch" - the first link. - Uraty
  • It is not very clear what you mean by this (I am not a magician, I only study). thanks for the answer. But in the test it is said in one block catсh ... Since you showed the answer “yes” suggests itself.
  • Ahead of a bit. Thanks - user293633
  • @Uraty Wrong for what? ) - ezhov_da
  • @ user293633 If someone explains to me and shows the necessity and usefulness of using this kind of sugar: NullPointerException | IOException e I will be only for, in other cases my example is the most commonly used practice, since there is a catch of any and unchecked including errors. - ezhov_da