* 抛出异常 使用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
您可能关注的文档
- HXD3型机车操作使用办法精选.doc
- HyperMesh入门教程精选.ppt
- Hyper-v虚拟机与系统安装配置管理实验精选.doc
- ICPKGPROCESS精选.ppt
- ICS-DT-6使用说明书精选.doc
- ICU病人的镇痛治疗精选.doc
- ICU中的评分系统精选.ppt
- IC封装流程精选.ppt
- IE(工业工程)手法--防错精选.ppt
- IE模特法-精益生产精选.ppt
- 2025年全国演出经纪人员资格认定考试试卷带答案(研优卷).docx
- 2025年全国演出经纪人员资格认定考试试卷完整版.docx
- 2025年全国演出经纪人员资格认定考试试题库及完整答案.docx
- 2025年全国演出经纪人员资格认定考试试卷完美版.docx
- 2025年全国演出经纪人员资格认定考试试卷含答案(实用).docx
- 2025年全国演出经纪人员资格认定考试试卷及答案(各地真题).docx
- 2025年下半年内江市部分事业单位公开考试招聘工作人员(240人)备考题库附答案.docx
- 2025年全国演出经纪人员资格认定考试试卷及答案1套.docx
- 2025年下半年四川成都市郫都区面向社会引进公共类事业单位人员2人备考题库最新.docx
- 2025年下半年内江市部分事业单位公开考试招聘工作人员(240人)备考题库附答案.docx
最近下载
- 塔吊工作安全培训内容课件.pptx VIP
- 2024-2025学年天津市部分区七年级(上)期末历史试卷(含答案).docx
- 天津大学物理化学教学课件ppt合集.pptx
- 土豆种植时间和方法.doc VIP
- SMAR公司DT301密度计说明书.pdf
- 2、高鸿业宏观经济学教案.doc VIP
- 高杆灯取电照明装置.pdf VIP
- Unit 2 Expressing yourself Part B let's learn 教案 三年级英语下册 人教PEP版.docx VIP
- 【通用】马年猜猜乐PPT(猜成语)打印版【课件】.pptx
- PDCA循环-提高手术间无菌物品定位放置规范率PPT优秀案例.pptx
原创力文档

文档评论(0)