您好,欢迎访问三七文档
一、填空题1.声明异常的关键字是______________2.抛出异常的关键字是______________3.不需要声明的异常是______________二、选择题1.下列异常处理语句编写正确的是:()A)try{System.out.println(2/0);}B)try(System.out.println(2/0))catch(Exceptione)(System.out.println(e.getMessage());)C)try{System.out.println(2/0);}catch(Exceptione){System.out.println(e.getMessage());}D)try{System.out.println(2/0);}catch{System.out.println(e.getMessage());}三、程序阅读题1.阅读下面程序回答问题:importjava.io.*;publicclassClass1{publicstaticvoidmain(Stringargs[]){inta=5;intb=0;System.out.println(a/b);try{System.out.println(a=+a);System.out.println(a/b);System.out.println(a*a=+a*a);}catch(ArithmeticExceptione){System.out.println(除数为0,这是不行的!);}finally{System.out.println(finally被执行!);}System.out.println(异常已发生,但不影响程序的执行!);}}1)运行上述程序,输出结果是什么?(异常提示信息除外)答案:除提示异常提示信息外,无其他输出2)将变量b的初值改成5后,输出结果是什么?答案:1a=51a*a=25finally被执行!异常已发生,但不影响程序的执行!四、程序设计题1.如图输入两个数据,点击divide按钮,控制台显示两个数的商。当除数number2为0时,捕获异常,并在控制台上打印异常信息。答案:importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;publicclassTestException1extendsJFrameimplementsActionListener{privateJTextFieldnum1=newJTextField();privateJTextFieldnum2=newJTextField();privateJButtonbutton=newJButton(divide);publicstaticvoidmain(String[]args){TestException1frame=newTestException1();frame.pack();frame.setVisible(true);}TestException1(){Containercontainer=getContentPane();container.setLayout(newBorderLayout());JPanelp1=newJPanel();p1.setLayout(newGridLayout(2,2));p1.add(newJLabel(number1));p1.add(num1);p1.add(newJLabel(number2));p1.add(num2);JPanelp2=newJPanel();p2.setLayout(newFlowLayout());p2.add(button);container.add(p1,BorderLayout.CENTER);container.add(p2,BorderLayout.SOUTH);button.addActionListener(this);}publicvoidactionPerformed(ActionEvente){Stringn1=num1.getText().trim();Stringn2=num2.getText().trim();inti,j;intresult=0;try{i=Integer.parseInt(n1);j=Integer.parseInt(n2);result=i/j;System.out.println(result);}catch(Exceptionex){System.out.println(e);}/*finally{System.out.println(result);}*/}}2.从控制台获取两个字符串,并将两个字符串按照录入的先后顺序依次连接,然后将结果输出在控制台上。当其中有一个字符串为空或者两个均为空时,控制台输出(onestringatleastisnotget。答案:publicclassTestException2{publicstaticvoidmain(String[]args){try{Strings1=;Strings2=;s1=MyInput.readString();s2=MyInput.readString();if(s1.equals()||s2.equals())thrownewException();s1+=s2;System.out.println(s1);}catch(Exceptione){System.out.println(onestringatleastisnotget);}}}
本文标题:第十一章习题※答案
链接地址:https://www.777doc.com/doc-4754743 .html