Thursday, 15 January 2015

java - use def exception cannot be converted to throwable error -



java - use def exception cannot be converted to throwable error -

public class odexception extends exception { double deficit; odexception(string msg,double def) { super(msg); this.deficit=def; } } public void withdraw(double amt) throws odexception { if(amt<=balance) { balance=balance-amt; } else { throw new odexception("insufficient balance",amt-balance); } } public static void main(string ar[]) { try{ c[0].acc[0].withdraw(2000); } catch(odexception e) { system.out.println(e.getmessage()); } }

this want right?

class odexception extends exception{ double deficit; odexception(string msg,double def) { super(msg); this.deficit=def; } } public class customexception { double balance; public customexception(double balance) { this.balance = balance; } public void withdraw(double amt) throws odexception { if (amt <= balance) { { balance = balance - amt; system.out.println("withdrawn : "+amt+"\ncurrent balance :"+balance); } } else { throw new odexception("insufficient balance", amt - balance); } } public static void main(string ar[]) { seek { new customexception(1000).withdraw(5000); } grab (odexception e) { system.out.println(e.getmessage()); } } }

java exception-handling

No comments:

Post a Comment