Java Learning Lesson 20th (Exception application and Considerations (iii))

Source: Internet
Author: User


X. Application of anomalies

Import Java.util.random;import javax.rmi.corba.stub;/* * Teacher uses a computer class * Design two objects with object-oriented thinking problem * teacher, computer * * possible problems: * Computer blue screen, computer smoke * * Class Lan extends exception//blue screen {Lan (String problem) {super (problem);}} Class Fir extends exception//smoke {Fir (String problem) {super (problem);}} Class No extends Exception{no (String problem) {super (problem);}} Class computer{//defines the state of a computer running private int stu = 3;//stu is 1 normal, 2 is blue screen, 3 is smoke//stu = Random ()%2+1;public void Run () throws Lan,fir { Switch (stu) {case 2:throw the new LAN ("Computer is LAN!!!"); /There is no need to break because the case 3:throw new Fir ("Computer is FIR!!!") has been thrown; Case 1:SYSTEM.OUT.PRINTLN ("computer is running"); public void Rest ()//computer Restart {stu = 0; SYSTEM.OUT.PRINTLN ("Computer is Rest");}} Class Teacher{private String name = null;private computer C; Teacher (String name) {this.name = name; C = new computer ();} public void Lesson () throws Fir,no{try {C.run (); System.out.println (name+ "Teacher is teaching");} catch (Lan a) {System.out.println (a.tostring ());//Print information c.rest (); Lesson ();} catch (Fir b) {SYSTEM.OUt.println (B.tostring ());//Print the information test ();//can handle has been processed, unable to handle the continued outward throw//find someone, the computer maintenance, and inform the school and with reason, do not tell, is abnormal exception throw new No (" Class cannot be carried out, "+b.getmessage ());}} public void Test ()//do exercise {System.out.println ("Student is Exciesing");}} public class Main{public static void Main (string[] args) {Teacher B = new Teacher ("BLF2"); try {B.lesson ();} catch (Fir e) {S Ystem.out.println ("..."); catch (No e) {System.out.println (e.tostring ());} finally {//no matter what happens in class, but will still be dismissed System.out.println ("to the Class");}}

/*
* Exception conversion, also known as the exception of the package
* Database
* Class Nosuess extends Exception
* {
* ...//To convert an exception, simply tell the exception to occur
* }
* void AddData () throws Nosuess
* {
* 1. Connect to the database
* Try
* {
* Add data, exception SQLException, but the person who added the data doesn't know what SQLException is.
* }
* Catch
* {
* Internal processing
* throw new Nosuess ();
* }
* Finally
* {
* Close the database
* }
* }
* */


XI. Exceptions and Notices

(1). When a subclass overrides a parent class method, the method of the parent class throws an exception, and the child class's method can only throw the exception of the subclass or the subclass of the exception (see code below)

(2). If the parent throws more than one exception, the child class can only throw a subset of the parent class exception

Simply put: The subclass overrides the parent class to throw only the exception of the parent class or the subclass or subset of the parent exception

Note: If the method of the parent class does not throw an exception, then the subclass's overwrite must never be thrown


The following code explains the precautions (1)

Class A extends Exception{}class B extends A{}class C extends Exception{}class fu{void Show () throws A{}}class test{void me Thod (FU f) {try{f.show ();} catch (a) {}}}class ZI extends fu{void show () throws A, b//can also not be thrown, but can not throw other exceptions {}}public class main{public static void Main (S Tring[] args) {Test t = new Test (); T.method (new ZI ());//Use polymorphism to explain why you cannot throw other exceptions}}

The following code explains the precautions (2 )

Interface in{abstract void Show (); Class BB implements In{public void Show ()//throws Exception, because the parent class does not have an exception, the subclass cannot be thrown {//If there is a real exception, then only try, not throw}}

Abnormal to this end, a lot of content, I need to review more, digestion is ...


Java Learning Lesson 20th (Exception application and Considerations (iii))

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.