Java Se Basic Series (note)--Exception

Source: Internet
Author: User
Tags throw exception java se

Exception

A 1.java exception is a mechanism provided by Java to handle errors in a program, which are some unusual events that occur during a program's run.

2.java the execution of the program if an exception event occurs automatically produces an exception class object, which encapsulates the information of the exception event and is submitted to the Java Runtime system, this process is called throw exception;

When the Java runtime system receives an exception object, it looks for the code that can handle the exception and handles it, a process called a catch exception

1 public class TestEx1 {2 public     static void Main (string[] args) {3         TestEx1 test = new TestEx1 (); 4          5         // Catch Exception 6         try {7             test.m (0); 8             System.out.println ("a");    Will not be executed 9         } catch (ArithmeticException e) {             e.printstacktrace ();             //system.out.println (e);         //try{} contains code that may be wrong, if there is an error, he will hand over to catch{} processing, does not affect your code after the operation of         A//, but if you try{} There are several lines of code in the first line, the second row, the third row, If the second line goes wrong, the third line is not executed         .//If there is finally{},finally inside the code is to deal with the funeral. For example, if you have an operation workflow in your try, then if something goes wrong fianlly18         //Can close the workflow object and avoid wasting resources. finally{} is mandatory, whether it is an error or error.         System.out.println ("B");    will be executed     .}22     //declares that the method may throw an exception of $ public     void m (int j) throws ArithmeticException {25         ///manually throw out the    exception         if (j = = 0) {             arithmeticexception new ("Denominator cannot be 0");     }30}

3. In a try statement block, the capture statement of the parent exception can not be written on top of the subclass exception capture statement

4. The overriding method needs to throw the same exception as the original method or do not throw

5. Custom Exceptions:

-1 Declaring your own exception class by inheriting the exception class

2 Declare the method may throw an exception in the declaration section of the method with the throws statement

3 generate an instance of the custom exception in the appropriate location of the method and throw it with a throw statement

 1 public class TestEx3 {2 public static void Main (string[] args) {3 TestEx3 t = new TestEx3 (); 4 T. Manager (); 5} 6 7 public void Manager () {8 try {9 regist ( -3); ten} catch (MyException e) {1         1 System.out.println ("Registration failed, error type code:" + E.getid ()); E.printstacktrace (); 13}14 15 System.out.println ("End of Operation"); 16}17 18//In the declaration portion of the method, declare the exception that the method might throw by using the throws statement: public void regist (int num) thro WS MyException {20///Generate an instance of the custom exception in the appropriate location of the method and throw the IF (num < 0) with the throw statement {the new myexception ( "Negative numbers, unreasonable", 3);}24 System.out.println ("Number of registered persons:" + num); 25}26}27 28//Custom exception, by inheritance Java.lang.Exception class declares its own exception class MyException extends Exception {The private int id;31 the public myexception (String message, in      T id) {this.id = id;35}36 PNS public int getId () {id;39 }40}

Array

1 public class TestArr2 {2 publicly     static void main (string[] args) {3         date[] days = new Date[3], 4          5 for         ( int i=0; i<days.length; i++) {6             days[i] = new Date (7, i+1), 7         } 8 9 for         (int i=0; i<days.length; i++) {             days[i].in Fo ();         }12     }13}14 class Date {int     day;17     int month;18     int year;19     Public Date (int year, int month, int day) {         this.year = year;22         this.month = month;23         this.day = day;24     }25 -Public     void info () {         System.out.println (' Year: ' + year + ' month: ' + month + ' Day: ' + day ');     29}

Java Se Basic Series (note)--Exception

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.