Java-exception exercises

Source: Internet
Author: User

/* There is a circle and a rectangle. You can obtain the area. If an invalid area value occurs, it is regarded as an error in obtaining the area. The problem is indicated by an exception. The basic program is designed. * // Custom exception class NoValueException extends RuntimeException {NoValueException (String message) {super (message) ;}} interface Shape {void getArea ();} class Rec implements Shape {private int len, wid; Rec (int len, int wid) // throws NoValueException {if (len <0 | wid <0) throw new NoValueException ("negative"); // note newelse {this. len = len; this. wid = wid;} public void getArea () {System. out. println (len * wid) ;}} class Circle implements Shape {private int redius; public static final double PI = 3.14; Circle (int redius) {if (redius <= 0) throw new NoValueException ("negative"); this. redius = redius;} public void getArea () {System. out. println (redius * PI);} class ExceptionTest {public static void main (String [] args) {// try // {Rec r = new Rec (3, 4 ); r. getArea (); //} // catch (NoValueException e) // {// System. out. println (e. toString (); //} Circle c = new Circle (-3); c. getArea ();}}
Experience: Return to csdn after a long time of n. Find the familiar feeling again ~~

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.