java - Creating a custom exception class -
i new java programing , trying create custom exception class extends exception. uml class diagram given is:
unluckynumberexception
unluckynumber: int
unluckynumberexception(int) getunluckynumber():int
1) class needs extend class exception, assume is: public class unluckynumberexception extends exception
2) constructor should take argument "unlucky number" exception represents. value should stored instance variable in object (as int) * need help here *
3) need implement method getunluckynumber, gets called in main. simply returns "unlucky number" stored in object * need help here *
here phone call in main:
catch (unluckynumberexception e) { system.out.printf ("error: entered unlucky number: %d\n", e.getunluckynumber()); }
excetion:
public class unluckynumberexception extends runtimeexception { private final int unluckynumber; public unluckynumberexception(int unluckynumber) { this.unluckynumber = unluckynumber; } public int getunluckynumber() { homecoming unluckynumber; } }
main:
public static void main(string[] args) { seek { ... throw new unluckynumberexception(3); ... } grab (unluckynumberexception e) { system.out.printf ("error: entered unlucky number: %d\n", e.getunluckynumber()); } }
java class exception
No comments:
Post a Comment