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?

4
  • You could also create your own Exception Commented Apr 20, 2014 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. Commented Apr 20, 2014 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, not sure if it's considerable a too generic exception or not. Commented Apr 20, 2014 at 14:25
  • 1
    Possible duplicate of What exception to throw - "Wrong Scenario" (Java) Commented May 23, 2017 at 4:28

2 Answers 2

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).

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.