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

java学习之异常三

目录

一、throws

一、基本说明

 二、使用细节

二、自定义异常

一、 基本概念

​编辑二、自定义异常的步骤

三、实例

 四、练习

 三、throw和throws的区别

四、本章作业 

第一道

 第二题

 第三题

第四题


一、throws

一、基本说明

 

package com.hspedu.throws_;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;/*** @author GQQ* @version 1.0*/
public class Throws01 {public static void main(String[] args) throws FileNotFoundException{f1();}public static void f1() throws FileNotFoundException,NullPointerException,ClassCastException {//创建了一个文件流对象//1.这里的异常是一个FileNotFoundException 编译异常,必须要明确的处理//2.使用前面讲过的try-catch-finally//3.使用 throws ,抛出异常,让调用f1方法的调用者处理//4.throws可以抛出方法中产生的异常类型:FileNotFoundException,也可以抛出其父类Exception//5.throws 关键字后也可以是 异常列表,即可以抛出多个异常FileInputStream fis = new FileInputStream("d:\\aa.jpg");}
}

 二、使用细节

 

package com.hspedu.throws_;import java.io.FileInputStream;
import java.io.FileNotFoundException;/*** @author GQQ* @version 1.0*/
public class ThrowsDetail {public static void main(String[] args) {f2();//运行异常,默认throws处理}public static void f2() {//1.对于编译异常, 程序中必须处理, 比如 try-catch 或者 throws//2.对于运行时异常,程序中如果没有处理, 默认就是 throws 的方式处理int n1 = 10;int n2 = 0;double res = n1 / n2;}public static void f1() throws FileNotFoundException {//在f1()中调用方法f3(), f3()抛出一个编译异常: FileNotFoundException//编译异常必须要显式的处理,两种:t-c-f/throwsf3();}public static void f3() throws FileNotFoundException {//编译异常FileInputStream fis = new FileInputStream("d:\\aa.jpg");}public static void f4() {//1.在此处调用f5()是OK的//2.f5()抛出的是一个运行异常//3.运行异常有默认处理机制,并不要求显式处理f5();}public static void f5() throws ArithmeticException {//运行异常}
}class Father {public void method() throws RuntimeException {}
}class Son extends Father {//3.子类重写父类的方法时,抛出的异常类型要么和父类一致,或者是父类异常类型的子类型//子类抛出的异常类型 范围 <= 父类//4. 在 throws 过程中, 如果有方法 try-catch , 就相当于处理异常, 就可以不必 throws@Override//如果是throws Exception就会报错//如果是throws FileNotFoundException 也会报错因为这是编译异常,跟运行异常之间不存在继承关系public void method() throws NullPointerException {//NullPointerException是RuntimeException的子类}
}

二、自定义异常

一、 基本概念


二、自定义异常的步骤

三、实例

package com.hspedu.customexception_;/*** @author GQQ* @version 1.0*/
public class CustomException {public static void main(String[] args)/*throws Exception*/{int age = 124;if(!(age >= 18 && age <= 120)){//可以通过构造器,设置打印出的信息throw new AgeException("年龄需要在18-120之间");}System.out.println("年龄范围正确...");//如果只是扔出异常而没有catch,则不会执行此语句}
}
//自定义一个异常
//一般情况下,自定义异常是继承自RuntimeException
//2.即把自定义异常做成运行时异常,好处是: 我们可以使用默认的处理机制
//3.如果写成是extends Exception,则是编译异常,
// 4.就必须在main方法中显式的抛出异常 throws Exception,或者使用t-c-f
class AgeException extends RuntimeException{public AgeException(String message) {super(message);}
}

 

 四、练习

 

package com.hspedu.customexception_;/*** @author GQQ* @version 1.0*/
public class CustomExceptionExercise {public static void main(String[] args) {try{ReturnExceptionDemo.methodA();} catch (Exception e){System.out.println(e.getMessage());}ReturnExceptionDemo.methodB();}}
class ReturnExceptionDemo{static void methodA(){try {System.out.println("进入方法A");throw new RuntimeException("制造异常");}finally {System.out.println("用A方法的finally");}}static void methodB(){try {System.out.println("进入方法B");return;} finally{System.out.println("调用B方法的finally");}}
}

考察知识点:如果抛出了异常throw new RuntimeException("制造异常");,或者是出现了return语句就表示要结束此方法,剩余的代码不会再执行,但是如果有finally,那么finally中的代码必须执行,所以此时就会优先执行finally中的代码

 三、throw和throws的区别

四、本章作业 

第一道

 

我的代码

package com.hspedu.homework;/*** @author GQQ* @version 1.0*/
public class EcmDef {public static void main(String[] args) {/*编写应用程序EcmDef.java,接收命令行的两个参数(整数),计算两数相除。计算两个数相除,要求使用方法 cal(int n1,int n2)对数据格式不正确、缺少命令行参数、除0 进行异常处理数据格式不正确:NumberFormatException缺少命令行参数:ArrayIndexOutOfBoundsException除0:ArithmeticException*/try {//如果用这个循环条件的话,即使传入了3个参数也不会报错for (int i = 0; i < args.length; i++) {int n1 = Integer.parseInt(args[0]);int n2 = Integer.parseInt(args[1]);cal(n1,n2);}} catch (NumberFormatException e) {throw new NumberFormatException("数据格式不正确");}catch (ArrayIndexOutOfBoundsException e) {throw new ArrayIndexOutOfBoundsException("缺少命令行参数");}catch (ArithmeticException e) {throw new ArithmeticException("被除数为0,运算异常");}System.out.println("程序继续执行...");}public static void cal(int n1 ,int n2){System.out.println("res = " + n1 / n2);}
}

代码问题:

 在命令行输入参数时,数组args的数据就已经传入到main方法中了,所以

就算没有发生异常,此循环也会被执行  参数的个数   次,此时循环次数就是3

for (int i = 0; i < args.length; i++) {int n1 = Integer.parseInt(args[0]);int n2 = Integer.parseInt(args[1]);cal(n1,n2);
}

运行结果

 并且此代码只在   命令行参数只有一个的时候 会抛出异常,不能判断命令行参数 为多个或者0个的情况

正确代码

package com.hspedu.homework;/*** @author GQQ* @version 1.0*/
public class EcmDef02 {public static void main(String[] args) {//首先对传入的参数个数进行判断try {if(args.length != 2){throw new ArrayIndexOutOfBoundsException("参数个数不正确");//扔出异常后需要用try-catch来捕获异常}int n1 = Integer.parseInt(args[0]);int n2 = Integer.parseInt(args[1]);cal(n1,n2);} catch (ArrayIndexOutOfBoundsException e) {System.out.println(e.getMessage());;} catch (NumberFormatException e) {System.out.println("数字类型转换异常");}catch (ArithmeticException e) {System.out.println("数学运算异常(除数为0)");}System.out.println("继续执行程序...");}public static void cal(int n1, int n2){System.out.println("res=" + n1 / n2);}
}

 

 

 第二题

package com.hspedu.homework;/*** @author GQQ* @version 1.0*/
public class HomeWork02 {public static void main(String[] args) {//String[] args是一个空数组,里面没有存储任何数据System.out.println(args.length);//由于args是一个空数组,这里会发生ArrayIndexOutOfBoundsException//发生异常后,下面的代码都不会执行if(args[4].equals("john")){System.out.println("BB");}else{System.out.println("AA");}Object o= args[2];//ok,String是Object的子类Integer i =(Integer)o;//ClassCastException,Integer和String没有继承关系//String i =(String)o;//OK}}

 第三题

package com.hspedu.homework;/*** @author GQQ* @version 1.0*/
public class HomeWork03 {public static void main(String[] args) {try {func();System.out.println("A");//在try中如果抛出了异常,剩余代码块就不执行,所以此处不输出} catch (Exception e) {System.out.println("C");//捕获异常并打印 即第三步 C}System.out.println("D");//由于异常已经被捕获,所以可以正常输出 D,结果就是BCD}public static void func() {//静态方法try {//第一步是抛出异常,但是一旦抛出异常,就不会执行剩余代码//但是finally中的代码必须执行,所以先输出B,再抛出异常throw new RuntimeException();//第二步} finally {System.out.println("B");//第一步 B}}}

第四题

package com.hspedu.homework;/*** @author GQQ* @version 1.0*/
public class HomeWork04 {public static void main(String[] args) {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();//抛出异常}
}

http://www.lryc.cn/news/66569.html

相关文章:

  • 生产者向 Kafka 发送消息的执行流程
  • Linux命令·netstat
  • 《心安即是归处》读书笔记
  • C++:使用红黑树封装map和set
  • Go 命令
  • LEO、HW、LSO、LW 分别代表什么?
  • 问题 B: 跳石头(C++)(二分答案)
  • bugku——变量1
  • 网络数据包丢失监控
  • Linux服务器安装部署MongoDB数据库 - 无公网IP远程连接
  • CSS面试题:30道含答案和代码示例的练习题
  • 时间轮的golang实践浅析
  • Linux命令_stress 快速模拟CPU、内存、磁盘消耗
  • 可视化绘图技巧100篇分析篇(二)-生存曲线(LM曲线)
  • UP主发车啦!撩人仙侠文系列,谁来管管这个反派啊!
  • K8S使用持久化卷存储到NFS(NAS盘)
  • 一图看懂 multidict 模块:类似于字典的键值对集合,键可以多次出现,资料整理+笔记(大全)
  • django CBV 与 DRF APIView源码分析
  • 沃尔玛入驻教程:中国卖家如何免费、快速入驻沃尔玛walmart.com?
  • 《花雕学AI》Poe 上的四种 AI 机器人,你该怎么选?ChatGPT、Sage、Claude 和 Dragonfly对比
  • localStorage
  • 二十五、SQL 数据分析实战(9个中等难度的SQL题目)
  • JavaSE_02基本语法-编程单词词汇
  • 区间预测 | MATLAB实现QRDNN深度神经网络分位数回归时间序列区间预测
  • 如何使用aframe.js构建一个简单的VR播放器
  • Fiddler抓包工具常见功能介绍,还不会的进来看
  • 基于海鸥算法优化的核极限学习机(KELM)分类算法-附代码
  • JAVA代码规范审查
  • Centos8安装redis7
  • RabbitMQ详解(一):Linux安装