Java programming ideas --- Chapter XII through exception handling errors (on)

Chapter XII through exception handling errors

  The basic philosophy of Java is "Poorly structured code can not run." The ideal time to find errors at compile phase, that is, before you try to run a program, but can not find all the errors during compilation, the remaining issues that must be addressed during the operation, which requires sources of error can in some way, to pass appropriate information to a recipient, and the recipient will know how to deal with the problem properly.

 

12.1 The concept

 

  "Anomaly" is the word of "be surprised" means, the question arises, we may not know how to deal with, but we do know that should not be ignored, to stop and see if someone else can or elsewhere deal with this problem, but the current session do not have enough information to solve the problem, so to put this issue to a higher level of environment, where will make the right decision.

  Another anomaly brought about by the use of fairly obvious benefit is that he is often able to reduce the complexity of error handling code, if you do not use exceptions, then you must check the specific error, and in many places in the program to deal with it; and If you use exceptions, you do not have to be checked at the call method, because the exception mechanism will ensure the ability to capture this error, and error handling only in one place, the so-called exception handler, in this way not only saves the code, and the description doing the normal execution of code and a code problem in how to do phase separation. In short, compared to the previous error handling, exception mechanism makes reading the code, write and debug work more organized.

 

12.2 Basic anomaly

 

  It refers to prevent abnormal situations or problems the current method of scope to continue. The abnormal situations and common problems with distinction is important, so-called common issues refer to get enough information in the current environment, always handle the error. As for the abnormal situation can not go on, because they can not obtain the necessary information in the current environment to solve the problem, you can do is jump from the current environment, and to submit questions to the upper-level environment, which is thrown exception what happened. 0 is the divisor for division is a simple example.

  When an exception is thrown, first with Java to create other objects, as will be created on the heap using new exception object, then the current execution path is terminated, and references to pop exception object from the current environment, this time took exception handling mechanism program, and start looking for an appropriate place to continue the program. The right place is the exception handler. His task is to recover from the error in the program, so the program can be run in a different way, or continue to run.

 

12.3 catch the exception

 

12.3.1 try block

 

  If an exception is thrown, or other means inside the method call inside the method throws an exception, this method will end in the course of an exception is thrown in, if you do not want to end this method, the method can be set in a special block catch the exception, this is the try block:

try {
  //codes
}

 

12.3.2 exception handler

 

  Of course, the thrown exception must be somewhere in the process, this place is the exception handler, and for each of the exceptions to catch, have to prepare the appropriate handler:

try {
  //codes
} catch (Type id1) {
  //handle code of type1
} catch (Type id2) {
  //handle code of type2
}

 

Each catch clause is received and to receive only a particular type of a parameter of the method, the try block must be followed when an exception is thrown, the exception handling mechanism to search for a first type of abnormality processing parameter matches program, and then into the catch clause is executed, then regarded as abnormal have been addressed, once the catch clause ends, the process ends the search process program.

Guess you like

Origin www.cnblogs.com/parable/p/11574637.html