当前位置: 首页 > news >正文

异常 Exception 练习题 (未完成)

异常 Exception 练习题

  • try-catch异常处理
    • 1
    • 2
    • 3
    • 4
  • 异常
    • 1(没有自己写)
    • 2
    • 3
    • 4

try-catch异常处理

1

class Exception01 {public static int method() {try {String[] names = new String[3];//String[]数组if (names[1].equals("tom")) {//NullPointerExceptionSystem.out.println(names[1]);} else {names[3] = "hspedu";}return 1;} catch (ArrayIndexOutOfBoundsException e) {return 2;} catch (NullPointerException e) {//捕获return 3;} finally { //必须执行return 4; //返回4}}public static void main(String[] args) {System.out.println(method()); //4}
}

String[] names = new String[3];的意思是开辟了一个数组空间,每个元素都是一个String类,默认指向null。第五行if (names[1].equals(“tom”)) 的时候,name[1]是空指针,name[1].equals一定抛出异常,就会是NullPointerException,所以是return 3,但是这里有finally,还是会return 4.

2

public class TryCatchExercise02 {
}class Exception02 {public static int method() {int i = 1;try {i++; //i = 2String[] names = new String[3];if (names[1].equals("tom")) {//空指针System.out.println(names[1]);} else {names[3] = "hspedu";}return 1;} catch (ArrayIndexOutOfBoundsException e) {return 2;} catch (NullPointerException e) {return ++i; //i = 3} finally {//必须执行return ++i; //i = 4}}public static void main(String[] args) {System.out.println(method());}
}

OUT: 4

3

class ExceptionExe01 {public static int method() {int i = 1;//i = 1try {i++;// i=2String[] names = new String[3];if (names[1].equals("tom")) { //空指针System.out.println(names[1]);} else {names[3] = "hspedu";}return 1;} catch (ArrayIndexOutOfBoundsException e) {return 2;} catch (NullPointerException e) {return ++i;  // i = 3 => 保存临时变量 temp = 3;} finally {++i; //i = 4System.out.println("i=" + i);// i = 4}}public static void main(String[] args) {System.out.println(method());// 3}
}

NullPointerException 异常,catch中 i = 3; 但是先不return, 注意底层会保存在一个临时变量中 temp=3;
然后执行 finally,i=4,输出 i = 4。
然后回到NullPointerException 的catch中,还是会返回临时变量。
输出:
i=4
3

4

题目:如果用户输入的不是一个整数,就提示他反复输入,直到输入一个整数为止。

不会写!!!!ohhhhhhhhnononono/(ㄒoㄒ)/

public class TryCatchExercise04 {public static void main(String[] args) {//如果用户输入的不是一个整数,就提示他反复输入,直到输入一个整数为止//思路//1. 创建Scanner对象//2. 使用无限循环,去接收一个输入//3. 然后将该输入的值,转成一个int//4. 如果在转换时,抛出异常,说明输入的内容不是一个可以转成int的内容//5. 如果没有抛出异常,则break 该循环Scanner scanner = new Scanner(System.in);int num = 0;String inputStr = "";while (true) {System.out.println("请输入一个整数:"); //inputStr = scanner.next();try {num = Integer.parseInt(inputStr); //这里是可能抛出异常break;} catch (NumberFormatException e) {System.out.println("你输入的不是一个整数:");}}System.out.println("你输入的值是=" + num);}
}

异常

1(没有自己写)

a)编写应用程序EcmDef.java,接收命令行的两个参数(整数),计算两数相除。
b)计算两个数相除,要求使用方法cal(int n1, int n2)。
c)对数据格式不正确、缺少命令行参数、除0进行异常处理。

public class Homework01 {public static void main(String[] args) {/*编写应用程序EcmDef.java,接收命令行的两个参数(整数),计算两数相除。
计算两个数相除,要求使用方法 cal(int n1, int n2)
对数据格式不正确(NumberFormatException)、缺少命令行参数(ArrayIndexOutOfBoundsException)、除0 进行异常处理(ArithmeticException)。*/try {//先验证输入的参数的个数是否正确 两个参数if(args.length != 2) {throw new ArrayIndexOutOfBoundsException("参数个数不对");}//先把接收到的参数,转成整数int n1 = Integer.parseInt(args[0]);int n2 = Integer.parseInt(args[1]);double res = cal(n1, n2);//该方法可能抛出ArithmeticExceptionSystem.out.println("计算结果是=" + res);} catch (ArrayIndexOutOfBoundsException e) {System.out.println(e.getMessage());} catch (NumberFormatException e) {System.out.println("参数格式不正确,需要输出整数");} catch (ArithmeticException e) {System.out.println("出现了除0的异常");}}//编写cal方法,就是两个数的商public static double cal(int n1, int n2) {return n1 / n2;}
}

2

判断是否会发生异常?

public class Homework02 {public static void main(String[] args) {//args.length = 0//这里发生的是 ArrayIndexOutOfBoundsExceptionif(args[4].equals("john")){  //可能发生NullPointerExceptionSystem.out.println("AA");}else{System.out.println("BB");}Object o= args[2]; //String->Object ,向上转型Integer i = (Integer)o; //错误,这里一定会发生 ClassCastException}
}

空指针


可能发生NullPointerException
可能发生 ClassCastException

3

判断正误

public class Homework03 {public static void func() {//静态方法try {throw new RuntimeException();} finally {System.out.println("B");}}public static void main(String[] args) {//main方法try {func();System.out.println("A");} catch (Exception e) {System.out.println("C");}System.out.println("D");}}

B
A
C
D

注意: 不会输出A,因为在try代码块中 抛出异常之后是不会执行接下来的代码的。 正确答案是:B C D

4

判断正误

    public static void main(String[] args) {//main方法try {showExce();System.out.println("A");} catch (Exception e) {System.out.println("B");} finally {System.out.println("C");}System.out.println("D");}public static void showExce() throws Exception {throw new Exception();}}

B
C
D

相关文章:

  • 合并PDF出现OOM异常
  • Oracle SQL优化
  • 【小白进阶】Linux 调试大法——gdb
  • 软件测评中心▏软件集成测试和功能测试之间的区别和联系简析
  • 02、Tensorflow实现手写数字识别(数字0-9)
  • 在线文库系统 转码功能源代码展示 支持文档在线预览查阅功能
  • Linux “grep“ 命令
  • 【代码随想录】算法训练计划37
  • ctfshow刷题web入门--1--ljcsd
  • 键盘打字盲打练习系列之刻意练习——1
  • 异常数据检测 | Python实现孤立森林(IsolationForest)异常检测
  • RabbitMQ 安装(在docker容器中安装)
  • 微服务保护 Sentinel
  • Webpack的ts的配置详细教程
  • 【MATLAB】异常数据识别
  • 自己简单写的 事件订阅机制
  • [分享]iOS开发 - 实现UITableView Plain SectionView和table不停留一起滑动
  • 【Amaple教程】5. 插件
  • Android 架构优化~MVP 架构改造
  • Create React App 使用
  • FineReport中如何实现自动滚屏效果
  • JS进阶 - JS 、JS-Web-API与DOM、BOM
  • Median of Two Sorted Arrays
  • MySQL数据库运维之数据恢复
  • PhantomJS 安装
  • Python语法速览与机器学习开发环境搭建
  • vue数据传递--我有特殊的实现技巧
  • XForms - 更强大的Form
  • 巧用 TypeScript (一)
  • 腾讯优测优分享 | Android碎片化问题小结——关于闪光灯的那些事儿
  • 突破自己的技术思维
  • 赢得Docker挑战最佳实践
  • MiKTeX could not find the script engine ‘perl.exe‘ which is required to execute ‘latexmk‘.
  • ​用户画像从0到100的构建思路
  • (C++)栈的链式存储结构(出栈、入栈、判空、遍历、销毁)(数据结构与算法)
  • (C语言版)链表(三)——实现双向链表创建、删除、插入、释放内存等简单操作...
  • (ZT)北大教授朱青生给学生的一封信:大学,更是一个科学的保证
  • (二开)Flink 修改源码拓展 SQL 语法
  • (三) diretfbrc详解
  • (五)关系数据库标准语言SQL
  • .Mobi域名介绍
  • .net core Swagger 过滤部分Api
  • .NET core 自定义过滤器 Filter 实现webapi RestFul 统一接口数据返回格式
  • .NET 的程序集加载上下文
  • .Net(C#)自定义WinForm控件之小结篇
  • .net6+aspose.words导出word并转pdf
  • @Responsebody与@RequestBody
  • @RunWith注解作用
  • [ 云计算 | AWS 实践 ] Java 如何重命名 Amazon S3 中的文件和文件夹
  • [Android Pro] android 混淆文件project.properties和proguard-project.txt
  • [bzoj1038][ZJOI2008]瞭望塔
  • [C++]指针与结构体
  • [CF494C]Helping People
  • [C语言]——柔性数组
  • [hdu 1247]Hat’s Words [Trie 图]