异常(实际使用直接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{ }