Answer the question
In order to leave comments, you need to log in
Java: how to deal with exceptions correctly?
Now the topic of working with exceptions is holistic. Programmers are divided into those who are against exceptions, believing that their generation is too expensive an operation, and they should be used only in the most extreme cases, when there is no other way. Others believe that this is a very convenient tool, and not using it is stupid. I haven't joined either camp yet. I want to hear different opinions, and perhaps draw conclusions for myself.
Here are examples: let's say there is a method that looks for the occurrence of a substring in a string. It should return the index of the occurrence of this very substring in the string. And what should be returned if the method did not find the required substring? Maybe throw an exception, or return a specific number? Well, here, I think, it's clear - you need to return "-1".
But there are also more difficult examples. Here is my today's example from work: a card game is being written. There is a controller method that should assemble a deck from the received parameters. In the parameters, respectively, there are cards and their number. Internally, this method delegates to another class (deck class) the collection of a deck from the specified cards and their number. The bottom line is that the deck cannot have more than a certain number of cards of each type. The question is: what is the best way to proceed?
Method 1: in the controller method, check the incoming parameters for the allowed number of cards in the deck.
Method 2: in the deck class, in the method of collecting a deck of cards and their number, in case of exceeding the limit on cards, generate an exception, and then catch it in the controller.
The advantage of the first method is that no exception is thrown, there is no loss in performance due to this. But there are also disadvantages - in order to check these very limits on cards, you need to climb into the database, pull out the limits from there. And then, if the check was successful, when collecting the deck, these actions are repeated again. Those. we are doing the same job twice.
In the second method, everything is in one place: it’s logical, and the check takes place at the same time when the cards are pulled out of the database - you don’t need to go there several times.
Which way is better? And what advice can you give in general in such cases?
Answer the question
In order to leave comments, you need to log in
I think that in your case with maps, exceptions are not needed. Exceptions are needed when an error occurs and not when something is missing somewhere!
Let me give you an example: you have something that works with a database, for example, some kind of pool. You have 10 connections in the pool. When the connections run out, you can throw exceptions. Exceptions should be thrown when you cannot join the database. In short, when you can write an algorithm that works fine, then an exception is only thrown when an "unexpected" (expected) error occurs.
And specifically for your situation:
1. In general, forget about productivity until it turns out that it is in such and such a place that the bottleneck is.
2. The general principle is not to throw an exception unless the situation is exceptional.
For example, if it is in principle normal that there are more cards than necessary, then you do not need to throw an exception, but you need to handle this situation.
And if this should not happen, and all because the request is formed incorrectly, then you should throw an IllegalArgumentException.
I advise you to refer to the book Effective Java (the second edition seems to be fresh).
Exceptions can and should be used. Thinking about performance is not the first thing to do.
In your case, you need to understand whose mistake is the discrepancy in the deck. If this is a case that is straightforward in terms of business logic, which is hard to believe, then it should be processed with ordinary code, without exceptions. But most likely, method 2 is more organic in its essence of the exclusivity of the situation.
Second. IllegalArgumentException (or its successor) looks like a good fit.
In general, one should remember the words of D. Knuth:
Premature optimization is the root of all evil.In this light, the only advantage of the first method does not seem to be significant, and making the right choice is much easier.
I advise you to read some articles on this topic. This will allow you to better understand the mechanism of exceptions and formulate for yourself some rules for working with them.
For example, chapter 9 from the excellent book Effective Java .
Or here is another good article: www.ibm.com/developerworks/java/library/j-jtp05254/index.html
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question