say it today. Java exception handling mechanism, exception handling is not the first contact, especially written a lot C # code, the basic will be written to the exception-handling code, in fact C # the exception handling and Java the exception handling is basically the same, but in some details is not very much the same. Today, we'll talk about The exception handling mechanism for Java.
first, why need
1 , the reason is very simple is the mistake is unavoidable, why say: Our program is need to interact with the outside world, but the outside environment is no way to control, such as to access the file does not exist, suddenly network interruption, or operation error, etc. are unavoidable. And the code is written by the programmer, there is a bug is also reasonable.
2 , exceptions can not be avoided, but a good system can no longer encounter abnormal when the paralysis it?! We need to be friendly to the user and save the user's actions. This exception handling is very necessary.
First of all, we all know that it is unavoidable to write programs, but how are these anomalies caused?! Summary found that the exception is generally caused by user errors, program errors caused by physical errors.
second, the advantage
1 , the traditional method will put a lot of effort on error handling, if we can do the normal process with exception processing, and then the exception to capture processing
2 , traditional methods of normal processes and exceptions are mixed together, while exception handling is independent of each other, enhancing the readability of the code.
3 , the traditional method uses the branch statement to handle the error, increases the complexity of the code, and if the exception-handling method is used to classify the type of exception, it is simple and clear
4 , the traditional method can only deal with the errors considered, there is no response to unpredictable errors, if the exception is handled in the manner of the various exceptions can be processed
Third, classification
1,Throwable(java.lang.Throwable): InAPIThe document reads:Throwableclass isJavathe superclass of all errors or exceptions in the language. Only if the object is an instance of this class (or one of its subclasses)Javavirtual machines orJava Throwstatement is thrown. Similarly, only one of this class or its subclasses can beCatchthe type of the parameter in the clause.
2 , Error ( Java.lang.Error ): We all know it's wrong to mean it, A serious problem that indicates that a reasonable application should not attempt to capture
3 , Exception ( java.lang.Exception ): Exceptions refer to exceptions that the program itself can handle
4 Span lang= "ZH-CN" style= "Font-family:simsun", RuntimeException ( java.lang.runtimeexception ) (runtime Exception): May be in java exception superclass thrown during normal operation of the virtual machine. This exception can be avoided by improving the code.
5 , non-runtime exception (compilation exception): Yes runtimeexception exception, from the point of view of the program is the exception that must be handled, if not processed, the program cannot compile through. For example:sqlException and custom Exception exceptions.
iv. Abnormal mechanism
in the Java the two methods handle exceptions: Catching exceptions and throwing exceptions.
1 , catching Exceptions:
using try...catch to catch exceptions and handle them, multiple catch statements are used to match multiple exceptions, such as:
The code is as follows:
Package Com.tgb.pattern;import Javax.swing.joptionpane;public class Demo {public static void main (string[] args) {int i= Integer.parseint (Joptionpane.showinputdialog ("Please enter an integer")); Joptionpane.showmessagedialog (NULL, "The integer entered is:" + i);}}
If you enter QQ it will appear:
After adjustment:
Package Com.tgb.pattern;import Javax.swing.joptionpane;public class Demo {public static void main (string[] args) {try{ int I=integer.parseint (Joptionpane.showinputdialog ("Please enter an integer")); Joptionpane.showmessagedialog (NULL, "The input integer is:" + i);} catch (NumberFormatException nf) {Joptionpane.showmessagedialog (Null,nf.getmessage ());}}}
The result is:
This way the user can not understand, continue to modify:
Package Com.tgb.pattern;import Javax.swing.joptionpane;public class Demo {public static void main (string[] args) {try{ int I=integer.parseint (Joptionpane.showinputdialog ("Please enter an integer")); Joptionpane.showmessagedialog (NULL, "The input integer is:" + i);} catch (NumberFormatException nf) {Joptionpane.showmessagedialog (null, "Enter only integers!) ");}}}
The result is:
Look at the code for more than one catch:
Package Com.tgb.pattern;import Javax.swing.joptionpane;public class Demo {public static void main (string[] args) {try{ int I=integer.parseint (Joptionpane.showinputdialog ("Please enter an integer, range is 0-10:")); Joptionpane.showmessagedialog (NULL, "Number of inputs:" +getintdigit (i));} catch (Exception ex) { joptionpane.showmessagedialog (null,ex.getmessage ());}} static int getintdigit (int i) throws exception{ if (i>0 && i<=10) { return i; } else{ throw new Exception ("The range of input numbers is not between 0-10");}}}
The result is:
Failed to do exception handling for integer data, modify it below:
Package Com.tgb.pattern;import Javax.swing.joptionpane;public class Demo {public static void main (string[] args) {try{ int I=integer.parseint (Joptionpane.showinputdialog ("Please enter an integer, range is 0-10:")); Joptionpane.showmessagedialog (NULL, "Number of inputs:" +getintdigit (i));} catch (NumberFormatException nf) { joptionpane.showmessagedialog (null, "must enter Integer");} catch (Exception ex) { joptionpane.showmessagedialog (null,ex.getmessage ());}} static int getintdigit (int i) throws exception{ if (i>0 && i<=10) { return i; } else{ throw new Exception ("The range of input numbers is not between 0-10");}}}
The result is:
In this way, different exception information can be captured and processed, but the exception handling in the Getintdigit method of the above code is thrown and captured in the Main method. So I'm not going to write an instance about throwing exceptions anymore.
in the Java in the exception handling mechanism, there are many different Java exception Handling class, where there is no one by one description, can be in response to the API document, and of course, if you are interested, you can collect error messages during the code-tapping process. Good use of exception handling can improve the readability of the code, so that the code is more concise!
Exception handling mechanism of Java learning