The error mechanism in Java

Source: Internet
Author: User

Exception: Exception, program running process because of some reasons, so that the program can not run down note: In the case of the program is able to run, not the program compiled by example: Read the file, click a button, the file does not exist; Access to the database server, the database server power outage (1The occurrence of an exception is due to unforeseen circumstances in operation (2) The nature of the anomaly1) An exception occurs, the program terminates completely, and the following code is not run2When an exception occurs, the system encapsulates the exception in an object of a class, notifying the system to terminate (throws an exception). If software anomalies occur, the software will be very unstable and of low quality (3How is the exception handled? 1) surround the code that might appear to be abnormal with a try2The processing code after the exception is surrounded by catch and placed behind the try principle: the code in the try is run by the system, and if there is no exception, the code in the catch is not run, and if an exception occurs, the remaining code in the try is skipped and the code in the catch is executed instead. After the code in the catch is finished, execute the remaining code of the software note: a try can be followed by 1 or more exception types in Catch,catch, catch different exceptions, catch if the writeCatch(Exception e) captures all exceptions and a finally, used after catch, can not be used, up to a finally, save no matter whether or not an exception, must execute code example: Open a file, read the file, Close file connection in the process of reading a file regardless of whether or not an exception occurred, the work of closing the file must be done finally and is not dispensable! Finally, the code is executed once, as long as the entire software is still running, regardless of whether the current scope ends. If you do not put the code in the Finally, there is no such effect. Exception handling of the concept of abnormal occurrencesTry-Catch-finallytry only 1, catch1 or more, finally up to 1------Exceptions for other applications (1Example: Enter a number to show the square of the number by changing the program behavior by exception. However, if the input format is not a number, the input box will appear repeatedly until the input is correct. (2to encapsulate a program error message by customizing the exception: Write a function, enter an age,0-100, returns the age to the main function. Otherwise, "range error" is returned to the main function. In general, a throw flag in a function indicates that it may throw an exception. At this point, the function definition should be prefixed with the throws tag 
// The occurrence of an exception: Enter a number that shows the square of the number import javax.swing.*; class exception1{    publicstaticvoid  main (string[] args) {        = Joptionpane.showinputdialog ("input");         int n = integer.parseint (str);                 int r = n * N;        System.out.println ("The result is:" + R);}    }
//The occurrence of an exception: Enter a number that shows the square of the numberImportjavax.swing.*;classexception1{ Public Static voidMain (string[] args) {intN=0; Try{String str= Joptionpane.showinputdialog ("Input"); N=integer.parseint (str); System.out.println ("ASDFJSADF"); }Catch(Exception e) {System.out.println ("Program has an exception"); }        intr = n *N; System.out.println ("The result is:" +R); }}
//The occurrence of an exception: Enter a number that shows the square of the numberImportjavax.swing.*;classexception1{ Public Static voidMain (string[] args) {intN=0; Try{String str= Joptionpane.showinputdialog ("Input"); N=integer.parseint (str); }Catch(NumberFormatException e) {System.out.println ("The program has a numeric format conversion exception"); }Catch(Exception e) {System.out.println ("Program has an exception"); }                        intr = n *N; System.out.println ("The result is:" +R); }}
 // finally    exception1{ public  static  void   main (string[] args) {  try  {System.out.println ( "Open File"       );  SYSTEM.OUT.PRINTLN ( "Read and write Files");       Integer.parseint ("A"  catch  (Exception ex) {SYSTEM.OUT.PRINTLN ("exception occurred" );  finally  {System.out.println ("Close File connection" ); }    }}
 // finally    exception1{ public  static  void   main (string[] args) {  try  {System.out.println ( "Open File"       );  SYSTEM.OUT.PRINTLN ( "Read and write Files");       Integer.parseint ("A"  catch  (Exception ex) {SYSTEM.OUT.PRINTLN ("exception occurred" );    } System.out.println ( "Close File connection" ); }}
// finally class exception1{    publicstaticvoid  main (string[] args) {       try  {       System.out.println ("Open File");       SYSTEM.OUT.PRINTLN ("Read and write Files");         return ;       } Catch (Exception ex) {}       finally {System.out.println ("Close File connection");}     }}
//(1) Change program behavior by exception.//Example: Enter a number to show the square of the number. However, if the input format//is not a number, the input box will appear repeatedly until the input is correct. Importjavax.swing.*;classexception1{ Public Static voidMain (string[] args) {intn = 0; String message= "Input";  while(true){          Try{String str=joptionpane.showinputdialog (message); N=integer.parseint (str);  Break; }    Catch(Exception e) {message= "Re-enter"; }        }        intr = n *N; System.out.println ("The result is:" +R); }}
//Write a function, enter an age, 0-100, return age//to the main function. Otherwise, "range error" is returned to the main function. classexception1{Static intSetage ()throwsexception{//The function may throw an exception        intInputage = 2000; if(inputage<0| | inputage>100) {Exception e=NewException ("Range error"); ThrowE//throws an actual exception        }        returnInputage; }     Public Static voidMain (string[] args) {//How do I get the content returned/thrown?         Try{            intresult =Setage ();        SYSTEM.OUT.PRINTLN (result); }Catch(Exception e) {System.out.println (E.getmessage ()); }    }}
The package name contents of the key packages in the JDK are  examples of Java.lang  core language packs, the most basic API  System, Integer, mathematical operations java.awt  Abstract Window Toolkit, Generate graphical User interface  button, interface java.awt.event  event Handling Package  button Click event javax.swing  richer graphical user interface to generate  a button with icon Java.util  Toolkit  random number, date Javax.io  Input Output Package  file read/write Javax.net  Network programming support Packet  network transmission
Import Import. For example, when we use "Systerm.out.println ()", We never use " import Java.lang.System" To import the System class
For the package, we can generally observe its tree structure, for classes, we generally observe its content.

In addition, we can view the basic contents of a class. In general, we can select a link to a class in the lower left window, and the link to this class is displayed in the right window. If we select the "button" class in the "java.awt" Package (first select "java.awt" in the upper-left window, and then select "button" in the lower-left corner)

In the right window, the inheritance and basic usage of the Button class are listed first, where the reader can see the members of the class, labeled as follows:

constructor, indicated by the following tag:

member functions, marked with the following tags:

And the members inherited from the parent class, marked with the following markup:

The error mechanism in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.