Search code examples
javaexceptionerror-logging

Why throw the same Exception and do nothing with Log result?


At work a goal of mine is to be more involved in code reviews since I went to school for computer science and studied theory, etc. My coworkers all have picked up java through courses supplied from work, and just program to get an end product, not always in the most efficient way.

Anyways, we have a class that for our programs to access the sql database. Each method, throws a DataSourceException. With in that method there is also a try catch finally to handle the SQLException. This line is what intrigues me.

catch (Exception ex) {
      final String METHOD_NAME = "prepare";
      Log.log(MyClass.class, METHOD_NAME, ex);
      throw new DataSourceException(ex);
}

Why throw another DataSourceException if the method signature throws it? The result of the log isn't thrown. Meaning we do nothing with the line that contains Log.log : it essentially creates an object and never does anything with it? Should that be passed as the DataSourceException parameter. Sorry if this is a repeat.

TL;DR is the above code an anti pattern?


Solution

  • Looks like it's intended to log exceptions, and then rethrow them so that someones code has to actually handle them.