第七章
来源:互联网 发布:p2p网络管理 编辑:程序博客网 时间:2024/05/16 19:28
1.不对。
2.
public class yichang {public static void main(String[] args) { System.out.print("Please input an Integer: "); int value = new Scanner(System.in).nextInt(); try{ if(value < 0){ throw new MyException("My Exception comes!"); } }catch (MyException myExp){ System.out.println("MyExcepton caugth!"); } }}class MyException extends Exception{ public MyException(String s){ System.out.println("MyException occurs. The value must be greater than ZERO!"); }}
3.当应用程序试图在需要对象的地方使用 null 时,抛出该异常。这种情况包括:调用 null 对象的实例方法。访问或修改 null 对象的字段。
将 null 作为一个数组,获得其长度。
将 null 作为一个数组,访问或修改其时间片。
将 null 作为 Throwable 值抛出。
4.输出: exception000
exception111
finished
去黑体输出:exception111
exception
finished
去斜体输出:exception000
finished
5.输出结果:13423
6.
public static void main(String[] args) { // 输入计算参数 Integer a = 2; Integer b = 1; try { double cc = b/(double)a; //这样才能计算小数,不会报除0错误 System.out.println("cc:"+cc); } catch (NullPointerException e) { System.out.println("空指针异常"); }catch (ArithmeticException e) { System.out.println("计算异常"); }catch (Exception e) { System.out.println("其他异常"); e.printStackTrace(); } }
7.
import java.util.Scanner;public class Input { public static void main(String[] args) { int index = 0; int[] array = new int[10]; int max,min; while (true) { if (index == array.length) { break;} Scanner sc = new Scanner(System.in); System.out.print("输入[" + (index + 1) + "]:"); try { array[index] = sc.nextInt(); index++; } catch (Exception e) {System.out.println("输入错误,重新输入!");} } max = array[0]; min = array[0]; for (int i = 1; i < array.length; i++) { if (array[i] > max) {max = array[i];} if (array[i] < min) {min = array[i];} } System.out.println("max = " + max); System.out.println("min = " + min); }}
8.throws TimeOutException
阅读全文
0 0
- 第七章
- 第七章
- 第七章
- 第七章
- 第七章
- 第七章
- 第七章
- 第七章
- 第七章
- 第七章
- 第七章
- 第七章
- 第七章
- 第七章
- 第七章
- 第七章
- 第七章
- 第七章
- 第一篇博客
- 优先级队列priority_queue
- 【bzoj5088】HDU 6000 Wash
- nginx(5)变量转载自章亦春
- Mac版IDEA快捷键配置
- 第七章
- 本机的虚拟机执行ifconfig,显示不出ip的解决方法
- Android SQLiteDatabase事务的简介
- Netty 权威指南笔记(四):架构剖析
- C++ 二维数组传参
- VS中配置Nuget
- (更新)数据挖掘,自学一年拿了腾讯京东华为网易YY的Offer,方法与建议
- suse12安装postgres9.3.2
- 【VS】项目属性配置