Where is the best place to make an exception in this example?

In the library that I write, I have some methods that will only be executed very rarely and on the fly. As I model a car, one of the (Exceptional) methods is a cracked engine, which will be very rare. Should I make an exception in this method? This will stop the vehicle from using.

I read the .NET Framework Frameworkโ€™s manual book, which says that an exception should only happen when the method cannot complete the execution. The actual method of hacking the engine will always be completed, but if one of my methods is called, for example, starting the engine (this method will store the number of engine starts as a means of increasing the load on the engine), and there is a call to breaking the engine, where (if anywhere ) should I throw an exception?

+4
source share
3 answers

Suppose an exception is thrown to highlight problems with code or runtime. This is actually normal system logic (in this sense, a hacked engine is the state of the model, not a problem with the code), so you should not drop it.

Instead, there should be some kind of feedback mechanism that makes sense in the context of the system you are modeling.

+4
source

If the system (or subsystem) is designed for test engines, and the state of the hacked engine is a normal state in the system, expected as a result of normal processing of the system, this is no exception. if the system is one for use in a production engine, where when the engine is hacked, the overall system can no longer execute it; โ€ข perform a constructive function (to lift the boxes, move the car forward, whatever), then the excluding engine is an exception,

if the overall system is a combination of both, then within the subsystem that functions as a tester of the engine (where the cracked engine is one of the states, the system is intended for control, monitoring, sending reports and diagnostics, etc.), this should be a certain state , and not an exception, but in the rest of the system this should be an exception ... Then in the interface between these two subsystems you will need to detect the hacked state of the engine and convert it to an exception (and / or vice versa)

+1
source

I believe your Car model should have a variable declared like this:

 bool isEngineCracked; 

Then when you call crackedEngine(Car car) method, it returns bool

This, in turn, will allow you to check the condition of the engine to make sure that it works or not.

0
source

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


All Articles