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

Java异常2

异常抛出的两种形式:

  1. 系统隐式抛出;int n=10/0;—隐式抛出一个异常;
  2. 手动抛出异常:throw new Exception();
import java.util.InputMismatchException;
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);Student1 st1=new Student1();st1.setId(10);st1.setName("小楼");System.out.println("输入性别");st1.setSex(sc.nextLine());st1.toString();}
}public Student1(String name, int id, double score) {this.name = name;this.id = id;this.sex = sex;}public int getId() {return id;}public String getName() {return name;}public String getScore() {return sex;}public void setId(int id) {this.id = id;}public void setName(String name) {this.name = name;}public void setSex(String sex) {// 方法一,隐式抛出异常// if (!"男".equals(sex)&&!"女".equals(sex)) {// this.sex="男";// } else {// this.sex=sex;// }// 方法二:手动抛出异常if (!"男".equals(sex) && !"女".equals(sex)) {try {// 这种运行异常,程序不要求必须处理throw new RuntimeException();// 手动抛出异常} catch (Exception e) {// TODO: handle exceptionSystem.out.println("输入的性别不合理");}} else {this.sex = sex;}}public String toString() {return "Student [name=" + name + "id=" + id + "score=" + sex + "]";}}

异常处理的两张方式
1、try……catch
2,throws

throws:方法声明异常,如果多个异常类型,用逗号分隔
public void setId(int id) throws InputMismatchException,ArithmeticException{}

import java.util.InputMismatchException;
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);Student1 st1 = new Student1();try {st1.setId(-1);// 此方法声明了异常,主方法进行处理} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}st1.setName("小楼");System.out.println("输入性别");st1.setSex(sc.nextLine());st1.toString();}
}class Student1 {private String name;private int id;private String sex;public Student1() {}public Student1(String name, int id, double score) {this.name = name;this.id = id;this.sex = sex;}public int getId() {return id;}public String getName() {return name;}public String getScore() {return sex;}public void setId(int id) throws Exception{//声明异常if (id>200||id<0) {throw new Exception();}else{}this.id = id;}public void setName(String name) {this.name = name;}public void setSex(String sex) {// 方法一,隐式抛出异常// if (!"男".equals(sex)&&!"女".equals(sex)) {// this.sex="男";// } else {// this.sex=sex;// }// 方法二:手动抛出异常if (!"男".equals(sex) && !"女".equals(sex)) {try {// 这种运行异常,程序不要求必须处理throw new RuntimeException();// 手动抛出异常} catch (Exception e) {// TODO: handle exceptionSystem.out.println("输入的性别不合理");}} else {this.sex = sex;}}public String toString() {return "Student [name=" + name + "id=" + id + "score=" + sex + "]";}}

异常的两种类型
1、RuntimeException;程序不要求必须处理
2、cheked异常,检查异常,程序运行之前必须做好处理措施,否者编译报错,Unhandled Exception type Exception

异常处理机制的好处
1、程序不会中断,处理异常之后可以继续执行
2、可以向用户提供友好的异常信息
3、将程序的正常流程与错误处理分开,有利于代码的编写与维护
/*
*方法的重写:子类声明的异常类型必须和父类一致,或是父类异常类型的子类。
*
*/

/*
*自定义异常:1.继承系统的异常类;
*public class SexException extends Exception {

   }2.添加构造方法;public SexException() {}public SexException(String message) {super(message);}

*/

*throw和throws:
*1)所在位置不一样:throw出现在方法体里;

  •         throws出现在方法声明处;
    

*2)后面跟的内容不一样:throw new Exception();异常对象;跟一个对象;

  •           throws Exception;异常类型;跟多个异常类型,用逗号分隔;
    

*3)功能不一样:throw:抛出异常;

  •      throws:当作异常处理方式,向上抛出异常。
    

1.运行时异常:
java.lang.ArrayIndexOutOfBoundsException;
java.lang.NullPointerException;
java.util.InputMismatchException;
java.lang.ArithmeticException;
java.lang.ClassCastException;

2.检查异常:
ClassNotFoundException
IOException

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

相关文章:

  • 2024熵密杯初始题2
  • echarts属性之title
  • VUE errolog, vue 错误集
  • 驱动开发系列13 - Linux tasklet用法介绍
  • redis实现分布式锁,go实现完整code
  • 解析日期、编码
  • 【Qt】QApplication::restoreOverrideCursor():恢复鼠标光标到原始状态的用法解析
  • 重生之“我打数据结构,真的假的?”--2.单链表(无习题)
  • 【有啥问啥】视频插帧算法技术原理详解
  • Leetcode148,109以及二者的合并 -> Tencent面试算法题 - 无序双向链表转BST
  • 【蓝桥杯选拔赛真题77】python计算小球 第十五届青少年组蓝桥杯python选拔赛真题 算法思维真题解析
  • 获取Hive表备注
  • 10.30学习
  • 什么是栈溢出
  • 在linux中arm-linux-gcc和/usr/bin/gcc有啥区别
  • 常用环境部署(二十二)——MySQL的数据库迁移到另一个机器上
  • 两台主机只能单方向ping通
  • redis windows 5.0 下载
  • 视频转gif怎么转换?6种视频格式转换简单方法分享,附操作截图!
  • StructRAG简介
  • java脚手架系列12-mongoDB
  • python四舍五入保留两位小数
  • 期权懂|有什么期权交易策略能够稳赚不赔的?
  • 笔记本脱机状态
  • Node.js:模块 包
  • 油动无人机动力测试台-60公斤级-Flight Stand 60 ICE
  • 给grasshopper中的python脚本电池加个标签
  • 别被忽悠了 Lua 数组真的也可以从 0 开始索引?
  • docker占用磁盘过多问题
  • [实时计算flink]使用Python依赖