Workers must be good, and must first make a tool. When doing a project, you must first debug the debugging preparation in the era of process programming.
The returned error code is to determine the error, but the disadvantage of this method is that if you write an error code (for example, if you write a certain action)
Hardware operations), then the end user will fall into the endless detection code, it may be this
Uint err = functionXX (...);
IF (err == xxx)
...
IF (err == xxx)
...
...
...
100 lines
Only this program can be corresponding and recovered when there is an accident, but because the error handling is too large, the end user is likely to be like this.
IF (FunctionXX ()! = Suceeded)
exit (0);
Such some may be able to restore the operations that can not be spared, (for example, the network connection is timeout, the end of the run is too too much, as long as a dialogue is displayed
Box is fine)
When it is object-oriented programming, it is undeniable that many people still touched the wrong, in fact, various object-oriented languages provide abnormalities.
Treatment (for example Object Pascal, C , this article is C as an example)
Here is some of my experiences about anomalies.
For a variety of users, it is not every wrong, especially when the group is developed, for example, a module is about file operation.
The programmer who wrote this module will pay attention to the abnormality of the file operation failed, and the programmer of the network module just knows the network.
The error handling of the wrong error, so we can define a class such a class
Class CBasicexception
{
CBASICEXCEPTION (...);
Char * getdescription ();
Uint getErrorcode ();
...
}
Then inherit several subclasses from this class
CFILEEXCEPTION, CNETEXCEPTION ...
Then inherited the detailed abnormality associated with the specific class of errors, such as files fail, space is not enough ...
You can inherit from cfileException, and the connection timeout .. Wait from CNETEXCEPTION, so that the error is classified throughout
Class level is like this
CBASICEXCEPTION
/ /
CFILEXCEPTION CNETEXCEPTION
/ / / /
Copenfailed ... ctimeout ...
Then correspond to each error code and the corresponding exception, and some less important information can be packaged in an abnormality, such as the file can't be opened.
Can be spaced enough or shared conflict ... can be put together, if you want to deal with some, such as the space is not enough, then from the corresponding base
Copenfired
Then your function should be implemented like this
CMYFUNC ()
{
...
Throw cxxx (...);
}
And in the corresponding call part can be written
Void Operation1 ()
{
Try {
Myfunc ()
}
Catch (CFILEXCEPTION & E) // Processing Files
{
Printf ("% s", E.GETDESCRIPTION ());
Throw; // Re-thrown, if you get it, you don't have to thrown it.
}
Catch (CNETEXCEPTION & E) // Network
{
...
Throw;
}
}
This way as you want to handle the abnormality.
But your main function should write this way.
void main ()
{
Try {
Opertion1 ();
Operation2 ();
Operation .....
...
}
Catch (cxxx & e) // Handling unsupported important abnormalities
{
...
}
Catch (CBASICEXCEPTION & E) // Treatment to your packaged error
{
...
}
Catch (...) // Other abnormalities, unpacking, such as 0
{
...
}
}
This will be reasonable.
And if you add an error location in an abnormal description, you can better, such as the wrong function
However, don't don't write an exception class, whether the Catch class is written on the subclass. Otherwise, the Catch of the subclass may never work.
such as:
Try {
...
}
Catch (CBASICEXCEPTION & E)
{
...
}
Catch (CFILEEXCEPTION (& E)
{
...
}
The following Catch will never be executed.
In addition, you can't return the value in the constructor and the destructor function, you can only use anomalies.