《Java程序设计基础》ppt chapter9.docVIP

  • 1
  • 0
  • 约2.43千字
  • 约 4页
  • 2021-09-12 发布于广东
  • 举报
第9章 一、选择题 2.B 3.A 10.D 应该StringIndexOutOfBoundsException 二、填空题 1. public void methodName() throws IOException 或__ public void methodName() throws Exception 2. return前 3. 出现异常 4. 抛出异常_ 5. Exception 6. throws 7. RunException 8. 非运行时 9. finally 10. ArrayIndexOutOfBoundsException 三、修改程序错误 1. catch(NullPointerException e) //这里有错误 改为: 删除 或 改为 public void test() throws NullPointerException 2. 改为: try{ System.out.println(lst.get(10)); }catch (IndexOutOfBoundsException e){ System.out.println(IndexOutOfBoundsException); } catch(Exception e) { System.out.println(OtherException); } 3.修改后: try { f.createNewFile(); } catch (IOException e) { System.out.println(创建文件异常!); }catch (Exception e) { e.printStackTrace(); } 4. 改为: File file = new File(); // file.createNewFile(); //直接在本方法把异常消化掉 try { file.createNewFile(); } catch (IOException e) { System.out.println(创建文件异常!); } 5. File file = new File(); //file.createNewFile(); try { file.createNewFile(); } catch (IOException e) { System.out.println(创建文件异常!); } 四、编写程序 1. import java.util.InputMismatchException; import java.util.Scanner; public class RuntimeDemo { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); try{ int num= scanner.nextInt(); } catch(InputMismatchException e){ System.out.print(输入不匹配异常!); } } } 2. import java.util.InputMismatchException; import java.util.Scanner; public class RuntimeDemo { public static void main(String[] args) { int[] num = new int[5]; Scanner scanner = new Scanner(System.in); int i = 0; while(scanner.hasNext()) { try{ num[i++]= scanner.nextInt(); }catch(InputMismatchException e){ System.out.print(输入不匹配异常!); break; } catch(ArrayIndexOutOfBoundsException e){ System.out.print(数组下标越界异常!); } } } } 3. public class ThrowDemo { static void sanjiao(int a, int b, int c) { try { if(!(a + b c a + c b b + c a ))

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档