异常(实际使用直接try-catch)

1.常见系统异常

异常

异常的解释

ClassNotFoundException

未找到要装载的类

ArrayIndexOutOfBoundsException

数组访问越界

FileNotFoundException

文件找不到

IOException

输入、输出错误

NullPointerException

空指针访问

ArithmeticException

算术运算错误,如除数为0

NumberFormatException

数字格式错误

InterruptedException

中断异常。

2.自定义异常

(1)定义异常类—继承Exception类

(2)在方法内抛出异常

throw new 异常类();

(3)声明方法存在异常

在方法头的尾部加上:throws 异常类列表

3.实战

1.假设学校开设舞蹈课程,输入男生、女生的数量,如果男生或女生的数量为0,则舞蹈课程无法开设,如果不为0,则根据男生/女生人数的多少计算男生/女生的partner数量。请用异常类处理数量为0的情况,重新编写下面的代码。

要求运行结果如下:

主函数

public class DancerLesson {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
try{
System.out.println("Enter number of male dancers:");
int men=input.nextInt();
System.out.println("Enter number of female dancers:");
int women=input.nextInt();
if(men==0||women==0)
throw new myExcpetion(men,women);
else
{
if(men>=women)
System.out.println("Each woman must dance with"+men/(double)women+"men.");
else
System.out.println("Each man must dance with"+women/(double)men+"women.");
System.out.println("Begin the lesson");
}
}catch(myExcpetion e) {
System.out.println(e);
}
}
}

异常类

public class myExcpetion extends Exception {
public String tostring() {
return "人数错误";
} public myExcpetion(int men,int women) {
System.out.print("Lesson is canceled.");
if(men==0&&women==0)
System.out.println("No student");
else if(men>0&&women==0)
System.out.println("No men");
else
System.out.println("No women");

结果

2.要求声明定义两个Exception的异常子类:NoLowerLetter类和NoDigit类

再声明一个People类,该类中的void printLetter(char c)方法抛出NoLowerLetter异常类对象,void

printDigit(char c)方法抛出NoDigit异常类对象。请补充下列代码中【代码1】到【代码6】

//ExceptionExample.java
public class NoLowerLetter // 类声明,声明一个Exception的子类NoLowerLetter
{
public void print() {
System.out.printf("%c",'#');
}
}
public class NoDigit extends Exception // 类声明,声明一个Exception的子类NoDigit
{
public void print() {
System.out.printf("%c",'*');
}
}
class People {
void printLetter(char c) throws NoLowerLetter {
if(c<'a'||c>'z') {
NoLowerLetter noLowerLetter= new NoLowerLetter(); // 创建NoLowerLetter类型对象
throw noLowerLetter; // 抛出noLowerLetter
}
else {
System.out.print(c);
}
}
void printDigit(char c) throws NoDigit {
if(c<'1'||c>'9') {
NoDigit noDigit= new NoDigit(); // 创建NoDigit()类型对象
throw noDigit; // 抛出noDigit
}
else {
System.out.print(c);
}
}
}
public class ExceptionExample {
public static void main (String args[ ]){
People people=new People( );
for(int i=0;i<128;i++) {
try
{
people.printLetter((char)i);
}
catch(NoLowerLetter e)
{
e.print();
}
}
for(int i=0;i<128;i++) {
try
{
people.printDigit((char)i);
}
catch(NoDigit e)
{
e.print( );
}
}
}
}

3.选择题

1)设有如下代码:

 try {
tryThis();
return;
} catch (IOException x1) {
System.out.println("exception 1");
return;
} catch (Exception x2) {
System.out.println("exception 2");
return;
} finally {
System.out.println("finally");
}

如果tryThis() 抛出 NumberFormatException,则输出结果是? (C)

A. 无输出

B. "exception 1", 后跟 "finally"

C. "exception 2", 后跟 "finally"

D. "exception 1"

E. "exception 2"

2)设有如下方法:

public void test() {
try { oneMethod();
System.out.println("condition 1");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("condition 2");
} catch(Exception e) {
System.out.println("condition 3");
} finally {
System.out.println("finally");
}
}

如果oneMethod正常运行,则输出结果中有哪些?(AD)

A. condition 1

B. condition 2

C. condition 3

D. finally

3) 设有如下代码:

public void fun () {
int i;
try
{
i=System.in.read ();
System.out.println("Location 1");
} catch (IOException e) {
System.out.println("Location 2");
} finally {
System.out.println("Location 3");
}
System.out.println("Location 4");
}

如果有一个IOException发生, 则输出有哪些?    (BCD)

A. Location 1

B. Location 2

C. Location 3

D. Location 4

4)  设有如下代码:

1 String s = null
2 if ( s != null & s.length() > 0)
3 System.out.println("s != null & s.length() > 0"); 4 if ( s != null && s.length() > 0)
5 System.out.println("s != null & s.length() > 0"); 6 if ( s != null || s.length() > 0)
7 System.out.println("s != null & s.length() > 0"); 8 if ( s != null | s.length() > 0)
9 System.out.println("s != null | s.length() > 0");

以下行中哪些会产生空指针异常。                (D)

A. 2,4

B. 6,8

C. 2,4,6,8

D. 2,6,8

5) 类Test1、Test2定义如下:

1.public class  Test1 {
2. public float aMethod(float a,float b) throws IOException {
3. }
4. }
5. public class Test2 extends Test1{
6.
7.

将以下哪种方法插入行6是不合法的。              (A)

A、float  aMethod(float  a,float  b){ }

B、public  int  aMethod(int a,int b)throws  Exception{ }

C、public  float  aMethod(float  p,float q){ }

D、public  int  aMethod(int a,int  b)throws IOException{ }

最新文章

  1. Excel中提取最大值的问题
  2. CSS 魔法系列:纯 CSS 绘制图形(各种形状的钻石)
  3. WPF 小技巧
  4. Latex中关于参考文献的一些经验
  5. 【BZOJ2013】【JSOI2008】球形空间产生器
  6. AutoResetEvent 运用
  7. Moss的使用
  8. 洛谷 P1656 炸铁路
  9. jQuery中两种阻止事件冒泡的区别
  10. 【转载】Android Studio jar、so、library项目依赖,原文链接http://zhengxiaopeng.com/2014/12/13/Android-Studio-jar、so、library项目依赖/
  11. 学习【Web前端开发修炼之道】总结
  12. fzu Problem 2128 最长子串(KMP + strstr 经典好题)
  13. 通过jettymain启动项目
  14. Neo4j学习笔记(2)——数据索引
  15. python类中的内置函数
  16. 2.13 table表格定位
  17. Beta阶段事后分析
  18. BCB 串口控件的使用 TComm
  19. Mybatis源码正确打开方式
  20. 【BZOJ4556】[Tjoi2016&amp;Heoi2016]字符串 后缀数组+二分+主席树+RMQ

热门文章

  1. C# 之 日常问题积累
  2. 埃氏筛法求素数&amp;构造素数表求素数
  3. 【320】Python 2.x 与 3.x 的区别
  4. dsm winscp 获得 root 权限修改上传文件
  5. unity5-GI是什么?
  6. Stop单个Coroutine
  7. Python MySQLdb连接报2003错误原因
  8. python设置格式模板
  9. Linux设备驱动模型底层架构及组织方式
  10. (字符串 键盘转换)Convert QWERTY to Dvorak -- zoj -- 5526