Reputation: 67213
In a library I am writing, I have some methods which would only be executed very rarely and on the fly. As I am modelling a vehicle, one of the (Exceptional) methods is a cracked engine, which would be very rare. Should I throw an exception in this method? This will stop a vehicle from being usable.
I read the .NET Design Framework Guidelines book and it states that an exception should only occur when a method cannot complete execution. The actual engine cracked method will always complete, but if one of my methods, such as start engine (this method will store the amount of engine starts as a means of accruing stress to the engine), is called and results in a call to cracked engine, where (if anywhere) should I throw an exception?
Upvotes: 4
Views: 158
Reputation: 146499
If you system (or subsystem) is designed to test engines, and the state of an engine being cracked is a normal state within the system, expected as a result of normal system processing, then it is not an exception. if the system is one for use in a production engine, where, when the engine is cracked, the overall system can no longer perform it;s design function (Lift crates, move the vehicle forward, whatever) then a cracked engine is an exception.
if the overall system is some combination of both, then within the subsystem that functions as an engine tester, (where a cracked engine is one of the states the system is designed to manage, monitor, report on and diagnose etc.), it should be a defined state, not an exception, and within the rest of the system it should be an exception... Then it is in the interface between these two subsystems that you will need to detect the cracked Engine State, and transform it into an exception (and/or vice versa)
Upvotes: 1
Reputation: 24316
I believe your Car
model needs to have a variable declared like so:
bool isEngineCracked;
Then when you make a call to the crackedEngine(Car car)
method it will return a bool
This in turn will allow you to do state checking of the engine to see if it's engine is functioning or not.
Upvotes: 0
Reputation: 4939
Think an exception should be thrown to highlight code or runtime issues. This is actually normal system logic (in this sense that a cracked engine is a model state, not a code problem), so shouldn't throw.
Instead, there should be some feedback mechanism that makes sense in the context of the system you're modelling.
Upvotes: 4