Java exception throw and Try,catch application example

Source: Internet
Author: User

1 classLanpingexceptionextendsException2 {3 lanpingexception (String msg)4     {5         Super(msg);6     }7 }8 9 classMaoyanexceptionextendsExceptionTen { One maoyanexception (String msg) A     { -         Super(msg); -     } the } -  - classComputer - { +     Private intstate=2; -      Public voidRun ()throwslanpingexception,maoyanexception +     { A         if(state==1) at         { -             Throw NewLanpingexception ("Lanping!!!"); -         } -         if(state==2) -         { -             Throw NewMaoyanexception ("Maoyan!!!"); in         } -System.out.println ("Run Bat"); to     } +      Public voidReset () -     { theState=0; *SYSTEM.OUT.PRINTLN ("Computer reset!"); $     }Panax Notoginseng } -  the classTeacher + { A     PrivateString name; the     Privatecomputer comp; + Teacher (String name) -     { $          This. name=name; $comp=Newcomputer (); -     } -      Public voidPrelect ()throwsmaoyanexception the     { -         TryWuyi         { the Comp.run (); -System.out.println (name+ "Speak"); Wu         } -         Catch(lanpingexception e) About         { $ System.out.println (e.tostring ()); - Comp.reset (); - Prelect (); -         } A         Catch(maoyanexception e) +         { the System.out.println (e.tostring ()); - test (); $             Throwe; the         } the     } the      Public voidTest () the     { -System.out.println ("Test yourself!"); in     } the } the  About classKandra the { the      Public Static voidMain (string[] args) the     { +Teacher pp=NewTeacher ("Cao"); -         Try the         {Bayi Pp.prelect (); the              the         } -         Catch(maoyanexception e) -         { theSystem.out.println ("..."); the         } the     } the}

Java exception throw and Try,catch application instance

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.