Learning Essentials
The role of Throw
The role of throws
class User{private int age;public void setage (int age) {if (age<0) {runtimeexception e = new RuntimeException ("cannot be negative") ;//Generate exception object throw e;//Throw exception object}this.age = Age;}}
Class Test{public static void Main (String args[]) {User user = new User (); User.setage (-20);}}
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/54/40/wKioL1R9XdPyGr3SAABkVIXEH98733.jpg "title=" 1.jpg " alt= "Wkiol1r9xdpygr3saabkvixeh98733.jpg"/>
=====================================================
class User{private int age;public void setage (int age) throws exception{//Declare setage function may appear Exception exception if (age<0) { Exception e = new Exception ("Age cannot be negative");//Generate exception object throw e;//Throw exception object}this.age = Ages;}}
Class Test{public static void Main (String args[]) {User user = new User (); Try{user.setage (-20);} catch (Exception e) {System.out.println (e);}}}
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/54/41/wKioL1R9X72TuEb3AAAuB4v_XQk451.jpg "title=" 1.jpg " alt= "Wkiol1r9x72tueb3aaaub4v_xqk451.jpg"/>
31java exception Handling (ii)