Repeated interception allows you to catch multiple exceptions in the same catch statement.

How and why to catch multiple exceptions in a single catch statement?

  • one
    Well, to group errors - tCode
  • one
    not to write the same thing for two types of errors, for example - Senior Pomidor
  • one
    In the book you are reading, hereafter, there is an answer to the first question and some information on the second. - post_zeew
  • @post_zeew you are right, but is this an obstacle for filling our beloved Knowledge Base? I dare say that I ask about (I try) what is not here on ruSO. Moreover, I certainly provide an answer to most of my questions. - TimurVI
  • @TimurVI, Certainly not. - post_zeew

2 answers 2

It is convenient when you need to list a lot of exceptions in one block:

 catch (IOException | SQLException ex) { logger.log(ex); throw ex; } 

Read more

    All the same, the original question was a bit different:

    How and why to catch multiple exceptions in a single catch statement?

    This is done for error typing, a simple example:

     try { FileReader fr = new FileReader("test.txt"); int i; while ((i=fr.read()) != -1){ System.out.print((char) i); } } catch(FileNotFoundException fnfex) { //не найден файл } catch(IOException ioex) { //ошибка чтения } 

    That is, the developer is given the opportunity to diagnose the error depending on the type of error that occurred.