Java Programming Fundamentals ---- abnormal

Exception:
  is not normal, the program does not normally occur during operation, for example, prior to array operations mentioned subscript bounds, like null pointer exception. In fact, the program arise. The problem described in terms of object-oriented thinking, and packaged in a subject.
  Because of the problems arising reasons, there is a problem name, and the like described in question present a plurality of attribute information. When the most convenient way of multi-attribute information is to appear this information package. Java is abnormal in accordance with object-oriented thinking object encapsulates the problem. This will facilitate operational issues and deal with the problem.
  These issues are classified. And these problems have in common content such as: every problem has a name, as well as information describing the problem, location problem, so you can constantly drawn up. The formation of abnormal system:
-------- java.lang.Throwable:
Throwable: can be thrown.
  | ---- Error: error, under normal circumstances, without writing code targeted treatment, usually jvm occur, the need for the program to be amended.
  | ---- Exception: abnormal, treatment can be targeted
either wrong or abnormal, they have concrete subclasses reflect every issue, their subclasses have in common, is to have the parent class name only as a suffix name of the subclass.

Features abnormal system:
anomalies in the system all the classes and objects are created with nature can throw, that throw and the throws keyword can be operated (only abnormal feature of the entire system have).
throw and throws Usage:
  throw is defined in function, for thrown objects.
  functions defined on the throws, a throw exception class that is thrown plurality, separated by commas.
In the development, if defined function, find this feature will be some problems, the problem should be marked when defining the function, so that the caller can use this feature when the pre-treatment is given.
How to mark it?
  By throws keyword completion, format: throws an exception class name, exception classes ...
After such labeling, the caller, when using this feature, you have to deal with, otherwise the compilation fails.
Code demonstrates: the method throws an exception

Here Insert Picture Description
  When there is throw an exception is thrown within a function objects, try not be processed, it must be declared in the function, or else fail to compile (except RuntimeException). That is to say, within a function If you throw RuntimeException exceptions, you can not declare the function.

The reason to declare: the need caller handle the exception.
If the abnormal function declaration, the caller needs to deal with, throws treatment may be try.
Note: Generally not throw RuntimException exception.

Exception two ways:
1, is detected at compile time exception:
  the exception at compile time, if not treated (without the try throws no), it will fail to compile.
Note: as long as the Exception and its subclasses are compiled when the detected abnormality.
2, the abnormality (not compile-time detection) Runtime:
  at compile time, does not require processing, the compiler does not check. The exception occurs, treatment is not recommended, so that the program stops, the code needs to be corrected. Exception which has a special subclass of RuntimeException, and is a subclass of RuntimeException abnormal operation, also said that this exception is not checked during compile time exception.
  If an exception is thrown in the run-time function, you need not be declared on the function.
The reason is not declared: do not need to deal with the caller, abnormal operation, has been unable to let the program continues to run, so let's call processing, direct the program to stop, the code amended by the caller.

Exception handling statements:
the try {
  to be detected codes;
}
the catch (Exception class variable name) {
  exception handling code;
}
{fianlly
  code that will be executed;
}

There are three binding formats:
. 1, the try {}
the catch () {}
2, the try {}
the finally {}
. 3, the try {}
the catch () {} the finally {}
Note: 1, finally defined normally closed source code, It should be released as a resource;
2, in the catch if System.exit (0); // exit system (jvm end), finally will not read.
Code demonstrates:
Here Insert Picture Description
Results of:
Here Insert Picture Description
acquiring information of an abnormal manner: Processing by the catch
the catch (Exception E) {// try E for receiving the abnormality detected objects.
  System.out.println ( "message:" + e.getMessage ()); // get information is abnormal.
  System.out.println ( "toString:" + e.toString ()); // get the name + is unusual about the exception.
  e.printStackTrace (); // print the abnormality information in the stack; abnormal abnormality information name + + abnormal position.
}

Exception handling principles: function of several throws exception if the function calls try process requires the corresponding catch block processing, such processing targeted, several polishing process on a few.
NOTE: When a corresponding plurality of try catch, the catch block if the parent class, must be placed below.
When defining exception handling, when the definition of try, when the definition of throws it?
  Internal functions if an exception occurs, if the interior can handle it with a try;
  If you can not deal with internal functions, you must declare it, let the caller handle. Use throws thrown to the caller process. Who calls this function who is the caller;

Custom exception:
  When the development project there is a problem in the absence of a definition of java, then we need to build in accordance with java abnormal thinking, will appear in the program-specific issues encapsulated object. This anomaly, called custom exception.
  For a division operation, 0 is not possible as a divisor. In java to this problem will be described with ArithmeticException class. For this function, in our project, in addition to the divisor is not 0, but can not be negative. But the negative part of java and not for the description. So we need to customize the exception.

Custom exception of steps:
1. Define a subclass inherits Exception or RuntimeException, let the class with disposable sex (either using the throw and throws to call such).
2, so that the class has common methods of operation exception.
When information is to define the custom exception can be used have been defined parent class function. The exception information passed to the constructor of the superclass.
the extends MyException class
  Exception {
    MyException (the Message String) {
    Super (Message);
  }
}

For example, a case where the divisor is 0 and negative custom exception:

Here Insert Picture Description
The result:
Here Insert Picture Description<>
Benefits abnormality:
1, the problem can be encapsulated;
2, the normal flow of processing code code and phase separation problems, easy to read.

Exception handling principles:
processing two ways: try or throws;
when calling the function thrown exception is thrown a few, a few to process (corresponding to a plurality of try catch); catch plurality of the catch, the parent class into the bottom of;
the catch, to define a targeted approach. Do not simply defined printStackTrace or output statement, or else do not write.

Conversion unusual idea: when the caller can not handle the exception is occurring, you need to convert this into an abnormal caller can handle exceptions thrown.
{the try
  the throw new new AException ();
}
the catch (AException E) {
  the throw E;
}
  if not exception handling, but does not belong to the abnormal function appears. After the conversion may be abnormal, and then throws the functionally related abnormalities.
  Or abnormal can handle, but functionally related issues and this needs to be generated to provide an exception out, let the caller know and deal with. After exception handling can also be captured, converted the new exception is thrown.
{the try
   the throw new new AException ();
}
the catch (AException E) {
// AException process of
  the throw new new BException ();
}

When an exception occurs when the child-parent classes covered, with some new features:
1, when the parent class subclass coverage, if the parent class exception is thrown, then the method subclass either do not throw abnormal either throw an exception or parent subclass of the exception, not throw other exceptions.
2, if the parent class or interface had not thrown an exception, then the subclass is not throwing an exception if the method covered by subclass appeared abnormal, can only try not throws.

Note:
If you can not handle this exception subclass has affected the specific operator class method, then you can subclass method, the throw throw a RuntimeException or a subclass, so that the subclass is not required throws statement.

When the throw alone, the following statement is not defined as unreachable.

Common exceptions:
1, the subscript bounds exception (an IndexOutOfBoundsException) comprises an array of strings;
2, type conversion exception: a ClassCastException
. 3, a null pointer exception: a NullPointerException
. 4, an abnormal operation is not supported;
abnormality should be avoided, if not avoided, it is necessary given treatment. Such as family medicine equipment, such as fire extinguishers.

  This article is based on the Auspicious teacher java foundation courses summary, please indicate.

Guess you like

Origin blog.csdn.net/weixin_39771344/article/details/91812703