6

I have a small Scala program which reads data from a data source. This data source is currently a .csv file, so it can contain data inconsistencies.

When implementing a repository pattern for my data, I implemented a method which will return an object by a specific field which should be unique. However, I can't guarantee that it will really be unique, as in a .csv file, I can't enforce data quality in a way I could in a real database.

So, the method checks whether there are one or zero objects with the requested field value in the repository, and that goes well. But I don't know Scala well (or Java for that matter), and the charts of the Java exception hierarchy which I found were not very helpful. Which would be the appropriate exception to throw if there are two objects with the same supposedly unique value. What should I use?

rumtscho
  • 2,474
  • 5
  • 28
  • 42
  • You could also create your own [Exception](http://stackoverflow.com/questions/3776327/how-to-define-custom-exception-class-in-java-the-easiest-way) – Ende Neu Apr 20 '14 at 14:17
  • @EndeNeu I could, but I don't care much about the type this time, as I am not planning to do anything special about it. I just thought that this is such a common scenario there is probably a convention about how to handle it, and that this is a good opportunity to learn the convention. Creating my own type won't teach me the convention, and will be overkill in this project. – rumtscho Apr 20 '14 at 14:21
  • I see, my knowledge of exceptions is not good enough to find a specific one for your case, probably I would use something like [IOException](http://docs.oracle.com/javase/7/docs/api/java/io/IOException.html), not sure if it's considerable a too generic exception or not. – Ende Neu Apr 20 '14 at 14:25
  • 1
    Possible duplicate of [What exception to throw - "Wrong Scenario" (Java)](https://stackoverflow.com/questions/20339167/what-exception-to-throw-wrong-scenario-java) – Basil Bourque May 23 '17 at 04:28

2 Answers2

14

There are two handy exceptions for such cases: IllegalStateException and IllegalArgumentException. First one is used when object internal state is in some illegal position (say, you calling connect twice) and the last one (which seems to be more suitable to your case) is used when there is the data that comes from the outside world and it does not satisfy some prescribed conditions: e.g. negative value, when function is supposed to work with zero & positive values.

Both are not something that should be handled programmatically on the caller side (with the try/catch) -- they signify illegal usage of api and/or logical errors in program flow and such errors has to be fixed during the development (in your case, they have to inform developer who is passing that data, that specific field has to contain only unique values).

om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
  • btw, "has to be fixed during the development" is the best way I've heard the truism "that the program could be reasonably expected to recover from" put. kudos. – djechlin Jan 13 '15 at 16:56
0

You can always use a customized Exception and in case this is a web API you might want to map your exception to: Bad Request (400) code.