Very tempting title, today is not for you to introduce, but to discuss some issues with you.
In the development of the past few years, large and small projects also experienced a lot, but no matter the project, there is no real exception to complete the process. Although we often see this kind of introduction when we are learning C #, do we really learn from it?
Let's see what exception is.
Exception's explanation is exceptional, and every time we are programmed, we will encounter it more or less (if your program has not met, you are a genius). Richer in its book, repeatedly mentions that exception is a violation of the original interface. At first I couldn't understand it again and again, when I really think about this problem, go into this topic, I understand, it is a violation, parameter error, object empty and so on our common exception, because we have the definition of the interface is violated, so the system will throw an exception.
Cases:
var str = "int";
int i = Convert.ToInt32(str);
This conversion throws an exception, although Convert.ToInt32 (String str) is a legitimate method, but the input str must be convertible to an int value, or throw an exception back. This is a violation of its method definition.
Exception Not equal to Error
The explanation of the error is very broad, may contain the exception, but exception is not equal to the error
A little bit of a brief introduction to the exception, I think many developers must be very familiar with it, but the real deal with it, we do not do well, many people think that the original system anomaly is enough for us to find bugs, we only need to test in place on it, but this is the wrong idea, it is also a terrible thought, Because you don't know when and where to throw an exception that you don't know, the Win form application has encountered an exception that is very scary and, if not handled, shuts down the application directly, in our web application, Although HttpApplication will be encapsulated in our unhandled exception, throw out a unhandlingexception, for you know, your site is facing different users, what do they see? Some people would say, I use the wrong page to wrap it, so it looks friendly. Yes, it's a solution, but have you ever thought about it? How are you going to know about these exception? Unless your site personnel encountered, will inform you in time, but if it is the customer encountered?
We can't wait, we are qualified programmers, our code is our children, we must give him the best, including his illness, we have to give him medical treatment, if not sick, we also have to fight vaccines, improve resistance, but also in time to think about a good deal, lest it will be hectic. Therefore, we must have a good solution mechanism to deal with all kinds of unexpected events.
Is there a really perfect solution? I can tell you for sure, No. No matter what application you develop, your developers, your environment, your programming habits, and your development processes all affect this solution and how to solve it, my point is: start with people.
The code is dead, the man is alive, we must always realize that we are born for the creation of procedures, not for the work to do, the beauty of the program, is the beauty of the process of the bug, is a human bug, a program of good or bad, in fact, can see a person's thinking. An exception to the application is not a developer's incompetence, which may be a good opportunity to experience, so that you can better learn to solve the problem.
In dealing with exception, we need to pay attention to a few points, we can see another "design of the exception solution of a few points of attention," This is recently seen in a book introduced, this is online to find the Chinese version, very good. These specifications have largely allowed us to deal with a lot of exception, however, the same sentence, from the beginning, developers of the application, the developer's thinking is very important, a company uniform technical specifications, is very necessary, in other code, you can be arbitrary, but in the handling of anomalies, You have to follow this specification, because he is really very important and very easy to ignore, each company should be a certain training, code review, are essential.
Unit Test
Unit testing is a solution, but not every unit test solves bugs in your system.
Said so much, in fact, I want to let you understand the importance of abnormal, but also let everyone in the development process, can think of these, lest cause unpredictable consequences.
Follow-up I will give you a certain solution, combined with the experience of these years, the development of a set of general-purpose exceptionhandling, of course, this also requires the support of my people: P