JAVA 20th (application of exceptions and precautions (3 ))

Source: Internet
Author: User

JAVA 20th (application of exceptions and precautions (3 ))


10. Abnormal applications

Import java. util. random; import javax. rmi. CORBA. stub;/** teachers use computers to attend classes * use object-oriented thinking * problem design two objects * teachers and computers ** 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 {// define 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 new Lan ("Computer is Lan !!! "); // Break is not required here, because case 3: throw new Fir (" Computer is Fir !!! "); Case 1: System. out. println ("Computer is running"); break ;}} public void rest () // restart the Computer {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 (. toString (); // print the information C. rest (); lesson ();} catch (Fir B) {System. out. println (B. toString (); // print the information test (); // the processed data has been processed. If not, the data can still be discarded. // find someone and repair the computer, and inform the school of the cause. If you do not tell me, it means that the exception throw new No ("the lesson cannot be performed," + B. getMessage () ;}} public void test () // do exercises {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) {System. out. println ("..... ");} catch (No e) {System. out. println (e. toString ();} finally {// no matter what happens in the class, it will still take the class System. out. println ("coming to class ");}}}

/*
* Abnormal conversions are called abnormal encapsulation.
* Database
* Class Nosuess extends Exception
*{
*... // Converts an exception. You only need to inform me of the exception.
*}
* Void adddata () throws Nosuess
*{
* 1. Connect to the database
* Try
*{
* When adding data, an exception occurs in SQLException. However, no one knows what SQLException is.
*}
* Catch
*{
* Internal processing
* Throw new Nosuess ();
*}
* Finally
*{
* Shut down the database.
*}
*}
**/


11. Precautions for exceptions

(1) When a subclass overwrites the parent class method, if the parent class method throws an exception, the subclass method can only throw the exception of the subclass or the subclass of the exception (see the following code)

(2) If the parent class throws multiple exceptions, the Child class can only throw a subset of the parent class exceptions.

To put it simply, The subclass overwrites the subclass or subset of the parent class that can only throw exceptions of the parent class or parent class exceptions.

Note: If the parent class method does not throw an exception, the Child class cannot be overwritten.


Notes for the following code explanation (1)

Class A extends Exception {} class B extends A {} class C extends Exception {} class FU {void show () throws A {}} class Test {void method (FU f) {try {f. show ();} catch (A a) {}}} class ZI extends FU {void show () throws A, B // You can also leave it empty, however, other exceptions cannot be thrown {} public class Main {public static void main (String [] args) {Test t = new Test (); t. method (new ZI (); // using polymorphism can explain why other exceptions cannot be thrown }}

Note for the following code explanation (2 )

Interface in {abstract void show ();} class BB implements in {public void show () // throws Exception, because the parent class has no Exception, the subclass cannot be thrown {// if an exception occurs, you can only try and not throw }}

This is the end of the exception. There is a lot of content. You need to review more and digest it...


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.