第9章 异常的.pptVIP

  • 1
  • 0
  • 约8.33千字
  • 约 34页
  • 2017-10-13 发布于浙江
  • 举报
第9章 异常的

throws关键字示例 public class Student {//定义学生类 private String mName; //姓名 private int mAge; //年龄 …… //其它方法,代码略 /*为姓名赋值的方法*/ public void setName(String name) { mName = name; } /*为年龄赋值的方法,该方法有可能抛出异常*/ public void setAge(int age) throws AgeException{ if (age 0 || age 100) { throw (new AgeException()); } mAge = age; } …… //其它方法,代码略 } 调用带有throws的函数1 当调用带有throws关键字的函数时,则必须放在try/catch块中进行监控,否则编译器将会报错; public class ThrowsTest { public static void main(String[] args) { Student std = new Student(); tr

文档评论(0)

1亿VIP精品文档

相关文档