首页 > 建站教程 > dart >  Dart编程实例 - 自定义异常正文

Dart编程实例 - 自定义异常

Dart编程实例 - 自定义异常

class AmtException implements Exception {

   String errMsg() => 'Amount should be greater than zero';

}  

void main() {

   try {

      withdraw_amt(-1);

   }

   catch(e) {

      print(e.errMsg());

   }  

finally {

      print('Ending requested operation.....');

   }

}  

void withdraw_amt(int amt) {

   if (amt <= 0) {

      throw new AmtException();

   }

}