C
C
CAMOKPYT2014-12-30 00:58:56
ruby
CAMOKPYT, 2014-12-30 00:58:56

How to properly use exceptions?

I decided to understand the details of how they work and how to use exceptions correctly. The problem is that you can almost always get away with if else, but sometimes exceptions are used, and I can't figure out in which situations which is preferable. We also care about the issue of performance (why not stuff them everywhere?) And security and "truism" for C #, Ruby and C ++ (as it correlates with the philosophy of the language).

Answer the question

In order to leave comments, you need to log in

6 answer(s)
X
xanep, 2014-01-01
@CAMOKPYT

habrahabr.ru/post/130611

V
Vitaly Arkhipov, 2014-12-30
@arvitaly

Everything is quite simple here.
Exceptions help to skip a section of code when certain conditions are met.
Moreover, with code without function calls, you can always replace it with if / else, but the code will be multi-nested (one if in another). But of course no one writes such code for a long time.
And with code with function calls, this becomes completely impossible (you can’t write the beginning if in one method, and the closing bracket in another). And we have to pass the entire if chain up the call stack.
The idea of ​​exceptions is as follows:
1. We have an algorithm that must work according to a given scheme. We do not check the correctness of the returned values ​​anywhere at the level above or the correctness of the execution of the level below - it mustexecute correctly or fail. This condition is born from the understanding of encapsulation - everyone is responsible for their own piece of code.
2. If at some point, the method (piece of code) responsible for a certain functionality understands that it cannot perform the operation assigned to it, it reports this to a higher level.
3. The level above can handle the exception, or (if it does not know how) - pass the exception even higher in the call stack.
Those. I summarize: we have a code that in 90% of cases should be processed according to one algorithm and in 10% of cases there may be situations when this algorithm in one specific part of the code will follow a different scenario.
Those. your task is to write code in such a way that exceptions are only a safety net, and not part of the main algorithm.
An interesting point in the implementation of exceptions in lisp: there you can execute the code that caused the exception again (for example, try to connect to the database a second time using the exception itself).

S
Sergey, 2014-12-30
Protko @Fesor

Well, let me give you a simple example. You have a function that should open a connection to a database / read from a file / connect to a socket ... in a word, any function that returns a certain resource, with the help of which something will be accessed in the future.
The function must return some result, and of a certain type. What will you do in case of an error? And how to notify the developer what exactly went wrong?
For this, an exception mechanism was introduced. If something goes wrong in a function, you can throw an exception with all the information available in error and throw it.
If you use the try / catch construct in the code that uses this function, then instead of a crash, one of the catch handlers will be called (you can go down the class hierarchy, setting different behavior for various kinds of errors), you may make some clarifications in the data, ask again user and the program will continue to run normally.

A
Alexey Zakharov, 2014-12-30
@Zakhar0v

if-else (switch) is used in cases
- "if not, then let's try from the other side",
that is, this is a choice of execution options.
and exception-
"if not so, then nothing (because ....)",
that is, the execution is stopped, and the start of some final actions (for example, write an error to the log and send a beautiful picture / stub to the user).
thus, "if" is normal mode, and exception is emergency mode

I
Illivion, 2014-12-30
@Illivion

I answer in C#. An exception must be thrown in any case if a method cannot perform the task it is assigned to. CreateUser() method failed to create a user? Exception! The Connect() method failed to connect within the given time interval? Exception. If I'm not mistaken, that's what Richter wrote.

A
alexanius, 2014-12-30
@alexanius

This design should not be shove anywhere. First, triggering an exception is a very expensive thing. Secondly, this should not be done, for example, in destructors. This is the first thing that comes to mind. Maybe there are other cases. I advise you to read what Meires writes on this topic. In general, I advise you to read it. My opinion is that an exception should be thrown if the program crashes and you need to somehow control the complete collapse.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question