JAVA abnormal handling and classification

  If a method can not be completed in accordance with the normal way, you're ready to exit through another path method, in which case throws an object encapsulates the error message, this time, this method will exit immediately and not return any value, other code that calls this method also can not continue, exception handling mechanism to execute the code to the exception handler.

  Throwable JAVA language is any errors or anomalies superclass, or error into the next layer Exception.

  Error:

  1. Error category refers to internal errors and resource depletion system error, the application will not throw objects in that class, if such an error, the application will try to make the program appears safe termination.

  Exception there are two branches, one is RuntimeException abnormal operation, a check is abnormal CheckedException. 

  RuntimeException such as: NullPointerException, ClassCastException; a check exception CheckedException, such as I / O errors due to IOException, SQLException. RuntimeException is the superclass of those exceptions that might be thrown during the normal operation of the Java Virtual Machine. If RuntimeException occurs, then it must be his mistake.

  

  Abnormalities CheckedException: General external error, such anomalies occur at compile time, Java compiler will force the program to catch such exceptions, will appear asking you to put this program may be abnormal try catch, abnormal class generally it includes several aspects:
  1. the end of the file tries to read the data;

  2. URL tries to open a malformed;

  3. Try to find the class object based on a given string, and the string representation of this class does not exist, and so on.
  Way to handle exceptions:

  1. Problems encountered no treatment, continue thrown to the caller, an exception is thrown, there are three ways, one throw, and second, throws, there is a system to automatically throw an exception,
  2.  1 public static void main(String[] args) {  
     2     String s = "abc";  
     3     if(s.equals("abc")) {  
     4       throw new NumberFormatException();  
     5     } else {  
     6       System.out.println(s);  
     7     }  
     8 }  
     9 int div(int a,int b) throws Exception{ 
    10 return a/b;} 

    thr catch catch the exception targeted approach.

  Throw and throws difference:

  

Different positions
   1. throws used in the function, is followed by an exception class with a plurality; and throw used within a function, is followed by an exception object.
Different functions:
   2. to declare throws an exception, let the caller know that the function issues that may arise can be given in advance of treatment; throw objects thrown specific problems, perform a throw, the function has ended, jump go to the caller, and the caller specific questions thrown objects. That is a throw statement exist independently, following Do not define other statements, because unreachable.

   3. throws represents a possibility of abnormal, not necessarily these anomalies occur; throw an exception is thrown, throw it must perform some kind of thrown exception object.
   4. Both are negative treatment of abnormal, or just throw might throw an exception, but not to deal with the abnormal function, real handle exceptions invoked by the upper layer function processing.

Guess you like

Origin www.cnblogs.com/Mr-RanX/p/11279667.html