From the contact anomaly I don't understand her, will not use her, want to in the system is the abnormal mechanism to play incisively and vividly, carried out a lot of attempts, the use of exception control procedures, the use of abnormal to do a number of judgment function, using exception to eliminate the system may appear annoying anomaly prompt box, In order to better use the exception to see a lot of articles about the exception
Until one day saw a sentence-"never to deal with you do not know how to deal with the exception", this suddenly dawned, feel that they have been using a powerful anomaly mechanism to do some heterodoxy is the matter, not to mention the status and significance of abnormal in the procedure, abnormal in fact a reporting mechanism, " She reports the problems in the program in an unavoidable way. She helps programmers to get to the right path, she faithfully provides error reporting to programmers, and she wants someone who can focus on and deal with the problems she reports, ha, I can't imagine, no abnormal mechanism how to prepare high-quality program! The following on the personal understanding and views of nonsense a few words, please criticize corrected, thank you!
The working principle of an exception, where there is a problem, immediately stop the current work, turn to exception handling code, if you can not find the exception handling code, to meet the exception to a report, the last layer received an exception will do the same thing, to the exception handling code, or the exception to report up, so that through the layers of errors passed out, Until one layer has handled the exception or has been reported to the user of the program--users. This layer is the call stack, when user a runs the program B,b start from function C, call function D, then call function E, then call function F, then f appears an exception, then this exception call stack is a (stack bottom)-〉b-〉c-〉d-〉e-〉f (stack top), This exception is reported along the stack from the top of the stack to the bottom of the stack, and if there is a handling code for this exception in function C, then the report chain of the exception is f-〉e-〉d-〉c. As you can see, if there is no code to handle this exception in the full call stack, user A is the end of the exception report, and an annoying message dialog box pops up to the Windows interface System.
Then user A to WHO report it, Haha, this is already not part of the scope of the program, the feeling will be the program as if God, telling the pain has let God all heard, it is satisfied with Haha, it seems that the program really pious haha. For the exception of this feature, can also be likened to subordinates to report problems, if subordinates misprision, the problem is serious, you if the leader know subordinates are such a 80% kicked him, on the contrary if you have a sound reporting mechanism subordinate team, haha you are awe-inspiring. Japanese enterprises in the clam has a purpose-contact, negotiation, report, in fact, is to let employees have to report to the superior habit. Now look at the program, haha, you do not have to instill in them what corporate culture, do not tell them what the importance of reporting, they are faithfully reported, if the programmer compared to corporate CEOs, then the program is to train a well-trained team of employees.
How to handle exceptions. There is a principle here that "never deal with an exception that you don't know how to handle", that is, only the exception that you know how to handle, the exception that you do not know must be airing, and actively report to the superior. What do you mean, know how to handle it? Let's talk about ways to handle exceptions, in general, the pop-up message box (this message box is different from the annoying Exception report message box, she is caught after the exception, according to the specific environment of the processing programmer to write the friendly message box), log the error log, Swallow, do the clean-up work, etc., So when there is an exception, stand in the position of the exception module and consider what kind of approach I should choose. If you cannot make a choice, choose not to process, that is, report to the superior.
For example, a function Fun1 is a method of creating and returning an active data connection object, and he accepts a database connection string, and if the caller (superior) gives him an incorrect connection string, FUN1 cannot create the connection object, resulting in an exception that creates no connection object. So what should he do with the anomaly then? Pop-up friendly message box? Say what friendly, Fun1 simply do not know what is the reason why he received the wrong connection string, play a "connection string error", the user must have to kill your heart, this hint and the user's business logic has a relationship! Log error logs, this is OK, but the recorded text is nothing more than "connection string is incorrect, connection string is: SQL ...", a good point, from the connection string to see the problem, in general, the code context to find the cause of the problem. This is not a bad way. Swallow, haha open what joke, you can not create a connection, and not squeak, want to let the caller crazy ah, this certainly not. Do the remedial work, line, really should clean up the scene, lest waste resources, but still did not squeak, so this way do not thorough. No, ha, in fact, the above analysis showed us a way to help us dispel the wrong choice, this road is to report up, or do not add any code, or log, do some clean-up, and then again the exception thrown.
So when you know how to deal with the exception, it depends on the actual situation and the user's request, this sentence is not said, like other headlines eye-catching but the conclusion is ambiguous article, haha, here can give a few suggestions,
1, generally, do not handle exceptions in the underlying module or method,
2, write common modules, DLLs, etc. are, can not use pop-up dialogs, such as relying on the platform, the framework of the way to handle exceptions,
3, when writing common modules, DLLs, etc., you must indicate in the documentation that each method attribute may throw an exception.
4, never write a try-like statement.
{} catch (Exception) {o Nothing} custom exception. Understand the principle of the exception and the mechanism, you can define their own exceptions, such practices often in the writing of controls, public modules, DLLs, etc., with the error number on the Internet search, can find a lot of information on the error code description. Most of them are made by M (Icro) S (OFT), MS from the operating system to a variety of frameworks have the number of various exceptions, for each exception made a detailed definition, if you also use such as spread and other commercial controls, you can see the various definitions of the exception inside him, That means we can define exceptions ourselves, and when necessary, we can make our own modules into a well-trained workforce. As for how to define an exception, specific language has a specific approach, such as C # to specify an exception from the exception inherited from the class, VB in the exception is a global variable, etc., see the language of interest to the grammar guide on it.
A new understanding of the anomaly, which many people have long thought to be very frightening, hateful, she is the wrong incarnation, she has annoying pop-up dialog box, so that the user and 凶煞 evil spirit of the ha, in fact, these are misunderstandings, the exception has been silently faithfully report the program in the serious unavoidable problems, she for the program, System of correctness, rigor call you, hope you pay attention to these problems, I hope you use wisdom to solve these problems, she is how lovely, and how noble, never because of her misunderstanding and give up their mission ... (Haha, nonsense, sensational ...) )。 It's important that we learn how to use her better.