The problem is this. In the class with the entry point there are try, catch blocks (this part of the code is shown below). I threw exceptions from other classes in catch and I would like to prologue each exception in a catch block. I looked at the documentation, screwed in the logger, but I don’t quite understand how to get each exception in one catch block in my case.

fileHandler= new FileHandler("Exceptions log.txt"); public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { try{ MailRepository mailRepository = new MailRepository(dbConnection); MailRepository dataModificator=new MailRepository(dbConnection); MailSender mailSender = new MailSender(dbConnection); List<Message> messageList = mailRepository.GetMessage(messageLimit); while(messageList.size() != 0){ messageList = mailRepository.GetMessage(messageLimit); for(int i=0; i<messageList.size();i++){ mailSender.send(messageList.get(i)); dataModificator.dataModification(Integer.parseInt(messageList.get(i).getId())); } } logger.addHandler(fileHandler); logger.setLevel(Level.ALL); logger.info("Start logging"); } catch(NumberFormatException |SQLException | NullPointerException | MessagingException e){ logger.log(Level.WARNING, "trouble sneezing", e); } logger.fine("Exception logged"); } }; 
  • one
    What does every exception mean? When an exception occurs, only one chain of exceptions occurs. In your case, if one of the following exceptions NumberFormatException | SQLException | NullPointerException | MessagingException : NumberFormatException | SQLException | NullPointerException | MessagingException NumberFormatException | SQLException | NullPointerException | MessagingException NumberFormatException | SQLException | NullPointerException | MessagingException information will be displayed in the log, with a glass trace. And Level.WARNING not particularly appropriate for exceptions, it’s still more appropriate Level.ERROR - iksuy
  • the first exception in case of a data format error, the second error in connecting to the database, or if something happened during the request. 3. if some field of the message object is empty and 4 exception in case of problems with sending messages. How to organize a log so that if an error occurs, he writes in the document, what happened, for example, SQLException, and not MessagingException? - Iga
  • one
    It will prescribe it anyway, because every exception that occurs has a message, and you give the exception itself to the log method as a third parameter. - iksuy
  • updated the question. it does not show which exception, only indicates the method that issued the exception - Iga
  • one
    so that you have updated no exceptions, at least there should be a trouble sneezing , but there is none. what is written to you line logger.fine("Exception logged"); which is outside the catch block. Are you sure that your program enters the catch block? Check out the debugger. - iksuy

1 answer 1

documentation

 try { MailRepository mailRepository = new MailRepository(dbConnection); MailRepository dataModificator=new MailRepository(dbConnection); MailSender mailSender = new MailSender(dbConnection); List<Message> messageList = mailRepository.GetMessage(messageLimit); while(messageList.size() != 0){ messageList = mailRepository.GetMessage(messageLimit); for(int i=0; i<messageList.size();i++){ mailSender.send(messageList.get(i)); dataModificator.dataModification(Integer.parseInt(messageList.get(i).getId())); } } logger.addHandler(fileHandler); logger.setLevel(Level.ALL); logger.info("Start logging"); } catch (NumberFormatException e) { logger.log(Level.WARNING, "trouble sneezing", e); } catch (SQLException name) { // обработка всех остальных эксепшенов по аналогии } ... logger.fine("Exception logged"); 
  • the documentation says that it is possible to handle several exceptions in a catch block 1 'catch (IOException | SQLException ex) {logger.log (ex); throw ex; } “Isn’t it possible in my case? - Iga