Ja15-08第9章异常处理.ppt

  1. 1、本文档共47页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
* 抛出异常 使用throw语句抛出的异常对象的语法格式为: 在一个方法内使用throw抛出异常对象,若该方法内部没有用try-catch对这个抛出的异常进行处理,则此方法应声明抛出异常,由其调用着处理。 异常抛出点后的代码在抛出异常后不再执行,也可以说异常的抛出终止了代码段的执行。 任何从Throwable派生的类都可以用throw语句抛出,抛出异常用来表明程序遇到的错误无法正常执行而需要异常处理 throw 由异常类所产生的对象; * class JavaThrow { public static void main(String args[]) { } } try { throw new ArithmeticException(); }catch(ArithmeticException ae){ System.out.println(ae); } try { throw new ArrayIndexOutOfBoundsException(); }catch(ArrayIndexOutOfBoundsException ai){ System.out.println(ai); } try { throw new StringIndexOutOfBoundsException(); }catch(StringIndexOutOfBoundsException si){ System.out.println(si); } * import java.util.Scanner; public class Dance1{ public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.println(Enter number of male dancers:); int men = keyboard.nextInt(); System.out.println(Enter number of female dancers: ); int women = keyboard.nextInt(); if (men == 0 women == 0) { System.out.println(没有一个学员.); System.exit(0); } else if (men == 0) { System.out.println(没有男学员.); System.exit(0); } else if (women == 0) { System.out.println(没有女学员.); System.exit(0); } System.out.println(有学员,可以开课.); } } 没有使用异常 的 例子 * import java.util.Scanner; public class Dance2 { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.println(Enter number of male dancers:); int men = keyboard.nextInt(); System.out.println(Enter number of female dancers:); int women = keyboard.nextInt(); try{ if (men == 0 women == 0) throw new Exception(没有一个学员.); else if (men == 0) throw new Exception(没有男学员.); else if (women == 0) throw new Ex

文档评论(0)

整理王 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档