User Defined Definitions: When Do We Use Them? What is an “exceptional” situation?

I'm sure this issue has been addressed in many best practice books, but still ... In most cases, I see examples of misuse of user exceptions, so I was wondering what would be a good situation to use them?

In particular, at the moment I am working on type checking for a compiler course. So I have a SymbolTable class that looks pretty much like a map. The key difference from your regular map is that each character has no more than one definition, so the put (String, Object) operation should fail if the key we are trying to insert is already in SymbolTable.

So, here's the question: whenever we try to insert a key, and that key already exists in SymbolTable, what should SymbolTable look like? If we have

boolean insert(String key, Object value); 

which returns false if insert fails, or should we use the insert method, which has a return value of void and throws an exception when a duplicate value occurs?

Thank you in advance:)

+4
source share
4 answers

Exceptions must be made in exceptional cases.

In this particular case, for example, if the method has the name insert() , I would consider the list already in the list as a normal case and update it.

In addition, although exceptions should not be used to control the flow of code, returning booleans indicating failure / success is not the best option (there may be many cases for failure, and False does not indicate anything).

On the bottom line, I would do something like the following:

 // Failures can happen void add(key, value) throws AlreadyOnMapException // Update if already on list void insert(key, value); // Make available Contains() methods to control the flow by avoiding exceptions boolean containsKey(key); boolean containsValue(value); 
+2
source

The decision about whether to use an exception or a return value is the balance of several "forces":

  • The caller can ignore the return value and continue with the next line, but cannot ignore the exception. Exceptions are generally used when ignoring the problem and continuing can lead to a bigger problem.
  • Exceptions put a strain on the caller because the code to process them is more complex than just checking the return value. Therefore, they are often avoided when a simple return value is sufficient.
  • On the other hand, exceptions can relieve the immediate caller of the burden of checking the result, because he can simply trust that if something goes wrong, the stack will be deployed to a catch block somewhere else.
  • Exception handling is more complex than regular feedback, so performance is triggered at their frequency. Exceptions are commonly used for situations that are not often expected.
  • When the error information and the “normal” result are of different types, throwing an exception is usually better than shoehorning unrelated things into the same return value (for example, to return the Object method) so that it can return either a string or a number).

In your example character table, I would probably just return false, as this might make the code simpler, but it might be reasonable, depending on the design of the rest of your program.

+2
source

Actually, it depends on what you decide. With your insert example, is it assumed that there is a duplicate, or would it be abnormal for there to be a duplicate? If the situation is abnormal (i.e. Something that usually should not happen), then this is exceptional.

What you should not do is use exceptions to control the flow. If, say, in 10% of cases it is expected that there will be a duplicate, then using the exception will be ineffective. But if it is a mistake on the part of the programmer (i.e., an unexpected action) to put a duplicate when it should not be inserted, perhaps an exception is appropriate.

This is discussed to the point of death, although try a little reflection.

0
source
 User defined exceptions: when do we use them? 

I am sure that there are very well-defined scenarios in which to use exceptions based on special rules in many books. Recently, however, I have observed an interesting use of operation-based exceptions to improve logging. for example, a function call function of class A is class B, and a function of class B calls a function of class C, and all these functions can throw an IO exception. therefore, if an IO Exception is part of a class C function, catch this exception, write it down, now create an object based on a special exception and throw that object away. each calling function will catch an exception based on a specific situation along with other exceptions, and whenever they catch an exception, they DO NOT register it because it will be redundant and they simply throw it away.

0
source

Source: https://habr.com/ru/post/1379070/


All Articles