Class weighttoomuchexception extends runtimeexception{private string message;public weighttoomuchexception (string message) {this.message=message;} Public string getmessage () {return message;} Public void setmessage (string message) {this.message=message;}} class weighttoolowexception extends runtimeexception{private string message; Public weighttoolowexception (string message) {this.message=message;} Public string getmessage () {return message;} Public void setmessage (string message) {this.message=message;}} Class man {private int weight;public void setweight (int weight) // Throws weighttoomuchexception,weighttoolowexception{if (weight > 100) {throw new Weighttoomuchexception ("You are too fat");} Else if (weight < 50) {throw new weighttoolowexception ("You are&Nbsp;too thin ");} This.weight=weight;} Public int getweight () {return weight;}} Class weightdemo2{public static void main (String[] args) {Man man=new Man (); Try{man.setweight (40);} catch (Weighttoolowexception ex) {System.out.println (Ex.getmessage ());} catch (Weighttoomuchexception ex) {System.out.println (Ex.getmessage ());}}}
This article is from the "Yehomlab" blog, make sure to keep this source http://yehom.blog.51cto.com/5159116/1784239
"DAY7" The seventh day of the exercise on the anomaly