程序在运行过程中出现不正常的情况
- java针对所有程序运行时出现的异常都包装了异常类
- 便于程序员调试,也便于编程中同统一处理异常
- 编译时异常:检查性异常、非运行时异常
必须在编程时进行处理,否则无法通过编译。在.java文件编译成 .class文件时出现的异常
IOException
FileNotFoundException
ClassNotFoundException
- 运行时异常:非检查性异常、非编译时异常
不必处理,在程序运行时由于虚拟机操作出现的异常
ArithmeticException
ArrayIndexOutOfBoundsException
ClassCastException
- 错误的父类java.lang.Error,异常的父类java.lang.Exception
- 错误一般情况下是jvm出现了不正常的情况,会导致程序崩溃,错误无法处理
try catch finally throw throws
- 通过逻辑判断规避异常,需要程序员编写大量的代码去补漏洞,容易遗漏、导致代码很复杂
- 使用try … catch … finally代码块处理异常
try:尝试执行可能有异常的代码
catch:用于捕捉可能发生的异常,可以有多个
finally:无论是否发生异常,都会执行。会在return之前执行。遇到System.exit(0);不会执行
多个catch捕捉的异常,必须从小到大
建议try代码块中的代码尽可能少,尽可能小的影响代码的执行效率,也便于异常定位
try必须有。catch和finally二选其一。都可以构成代码块
在可能出现异常的方法上声明可能出现的异常类
这个方法的调用者需要做统一处理
throw new Exception(“年龄在 0 - 200 之间”);
继承Exception创建编译时异常类
继承RuntimeException创建运行时异常类
//静态代码块的异常只能try
static {
File file = new File("d://test/txt");
try {
FileInputStream fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
//构造方法的异常可以抛出
public Demo02() throws FileNotFoundException {
File file = new File("d://test.txt");
FileInputStream fis = new FileInputStream(file);
}
public class Demo02 {
public static void main(String[] args) {
//test01();
//test02();
//test03();
test04();
}
private static void test04() {
try {
Student student = new Student("龙仔", -1, '中');
} catch (Exception2 exception2) {
exception2.printStackTrace();
}catch (Exception1 exception1) {
//System.out.println(exception1.getMessage()); 获取异常详细信息
exception1.printStackTrace();
}
}
private static void test03() throws ArithmeticException, InputMismatchException{
Scanner input = new Scanner(System.in);
System.out.println("请输入被除数");
int a = input.nextInt();
System.out.println("请输入除数");
int b = input.nextInt();
System.out.println(a / b);
System.out.println("end");
input.close();
}
private static void test02() {
Scanner input = new Scanner(System.in);
System.out.println("请输入被除数");
try {
int a = input.nextInt();
System.out.println("请输入除数");
int b = input.nextInt();
//1.通过逻辑判断规避异常
// if (b == 0){
// System.out.println("除数不能为0");
// return;// 结束当前方法
// }
System.out.println(a / b);
}catch (ArithmeticException e){
System.out.println("除数不能为0");
}catch (InputMismatchException e){
System.out.println("必须输入整数");
}catch (Exception e){
System.out.println("其他异常");
}finally {
System.out.println("finally");
}
System.out.println("end");
}
private static void test01() {
try {
Class clazz = Class.forName("org.jgs1904.demo.Demo01");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
package org.jgs1904.myexception;
/**
* @ClassName Exception1
* @Description 异常类1
* @Author RenYuWen
* @Since 2019/12/2 11:14
*/
public class Exception1 extends RuntimeException {
//自定义运行时异常
public Exception1() {
}
public Exception1(int age) {
super(age + "不在0-200之间");
}
}
package org.jgs1904.myexception;
/**
* @ClassName Exception2
* @Description 异常类2
* @Author RenYuWen
* @Since 2019/12/2 11:28
*/
public class Exception2 extends Exception{
//自定义编译时异常
public Exception2() {
super("性别只能是男或者女");
}
}
package org.jgs1904.entity;
import org.jgs1904.myexception.Exception1;
import org.jgs1904.myexception.Exception2;
/**
* @ClassName work01
* @Description 学生类
* @Author RenYuWen
* @Since 2019/12/2 10:57
*/
public class Student {
private String name;
private int age;
private char sex;
public Student(){
}
public Student(String name, int age, char sex) throws Exception1, Exception2 {
this.name = name;
if (age > 0){
this.age = age;
}else{
throw new Exception1(age);
}
if (sex == '男' || sex == '女'){
this.sex = sex;
}else{
throw new Exception2();
}
}
public char getSex() {
return sex;
}
public void setSex(char sex) throws Exception2{
if (sex == '男' || sex == '女'){
this.sex = sex;
}else{
throw new Exception2();
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) throws Exception1{
if (age > 0){
this.age = age;
}else{
throw new Exception1(age);
}
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", sex=" + sex +
'}';
}
}
package org.jgs1904.demo;
import org.jgs1904.myexception.Exception1;
import org.jgs1904.myexception.Exception2;
import org.jgs1904.entity.Student;
/**
* @ClassName Demo03
* @Description 测试类
* @Author RenYuWen
* @Since 2019/12/2 11:04
*/
public class Demo03 {
public static void main(String[] args){
Student student = null;
try {
student = new Student("龙仔", -1, '中');
} catch (Exception2 e) {
e.printStackTrace();
} catch (Exception1 e) {
e.printStackTrace();
}
}
}
文章浏览阅读2.6k次。解决Python词云库wordcloud不显示中文的问题2018-11-25背景:wordcloud是基于Python开发的词云生成库,功能强大使用简单。github地址:https://github.com/amueller/word_cloudwordcloud默认是不支持显示中文的,中文会被显示成方框。安装:安装命令:pip install wordcloud解决:经过测试发现不支持显示中文..._词云python代码无法输出文字
文章浏览阅读807次。扩展。_jmap 在线分析
文章浏览阅读1.1w次。随着炎热夏季的到来,当玩游戏正爽的时候,电脑突然死机了,自动关机了,是不是有想给主机一脚的冲动呢?这个很大的原因是因为CPU温度过高导致的。很多新手玩家可能都有一个疑虑,cpu温度多少以下正常?有些说是60,有些说是70,到底多高CPU温度不会死机呢?首先我们先看看如何查看CPU的温度。下载鲁大师并安装,运行鲁大师软件,即可进入软件界面,并点击温度管理,即可看到电脑各个硬件的温度。鲁大师一般情况下..._台式机玩游戏温度多少正常
文章浏览阅读243次。Day2-打印打印打印!我终于更新了!(哭腔)一、 最简单的打印最最简单的打印语句: print(“打印内容”)注意:python是全英的,符号记得是半角下面是我写的例子:然后进入power shell ,注意:你需要使用cd来进入你保存的例子的文件夹,保存时名字应该取为xxx.py我终于知道为什么文件夹取名都建议取英文了,因为进入的时候是真的很麻烦!如果你没有进入正确的文件夹..._puthon打印任务收获
文章浏览阅读1k次。centos8问题参考CentOS 8 EOL如何切换源? - 云服务器 ECS - 阿里云_"cenerrors during download metadata for repository \"appstream"
文章浏览阅读2.7k次,点赞3次,收藏11次。SpringBoot+Maven+MabatisPlusmaven在新建springboot项目引入RELEASE版本出错maven在新建springboot项目引入RELEASE版本出错maven详解maven就是通过pom.xml中的配置,就能够从仓库获取到想要的jar包。仓库分为:本地仓库、第三方仓库(私服)、中央仓库springframework.boot:spring-boot-starter-parent:2.2.1.RELEASE’ not found若出现jar包下载不了只有两_基于微服务的在线教育平台尚硅谷
文章浏览阅读316次。路由的概念路由器它称之为网关设备。路由器就是用于连接不同网络的设备路由器是位于OSI模型的第三层。路由器通过路由决定数据的转发。网关的背景:当时每家计算机厂商,用于交换数据的通信程序(协议)和数据描述格式各不相同。因此,就把用于相互转换这些协议和格式的计算机称为网关。路由器与三层交换器的对比路由协议对比路由器的作用:1.路由寻址2.实现不同网络之间相连的功能3.通过路由决定数据的转发,转发策略称为 路由选择。VLAN相关技术什么是VLAN?中文名称叫:虚拟局域网。虚_路由和vlan
文章浏览阅读2.8w次,点赞6次,收藏22次。设置div背景颜色透明度,内部元素不透明:.demo{ background-color:rgba(255,255,255,0.15) } 错误方式:.demo{ background-color:#5CACEE;opacity:0.75;} 这样会导致div里面的元素内容和背景颜色一起变透明只针对谷歌浏览器的测试_div设置透明度,里面的内容不透明
文章浏览阅读563次。1.[ u]文字:在文字的位置可以任意加入您需要的字符,显示为下划线效果。2.[ align=center]文字:在文字的位置可以任意加入您需要的字符,center位置center表示居中,left表示居左,right表示居右。5.[ color=red]文字:输入您的颜色代码,在标签的中间插入文字可以实现文字颜色改变。6.[ SIZE=数字]文字:输入您的字体大小,在标签的中间插入文..._discuzcode 大全
文章浏览阅读2.6k次。iOS中定时器有三种,分别是NSTimer、CADisplayLink、dispatch_source,下面就分别对这三种计时器进行说明。一、NSTimerNSTimer这种定时器用的比较多,但是特别需要注意释放问题,如果处理不好很容易引起循环引用问题,造成内存泄漏。1.1 NSTimer的创建NSTimer有两种创建方法。方法一:这种方法虽然创建了NSTimer,但是定时器却没有起作用。这种方式创建的NSTimer,需要加入到NSRunLoop中,有NSRunLoop的驱动才会让定时器跑起来。_ios nstimer
文章浏览阅读4.8k次,点赞17次,收藏51次。Linux的命令有几百个,对程序员来说,常用的并不多,考虑各位是初学者,先学习本章节前15个命令就可以了,其它的命令以后用到的时候再学习。1、开机 物理机服务器,按下电源开关,就像windows开机一样。 在VMware中点击“开启此虚拟机”。2、登录 启动完成后,输入用户名和密码,一般情况下,不要用root用户..._ls-lmore
文章浏览阅读4.1k次。1.登录MYSQL系统命令打开DOS命令框shengfen,以管理员的身份运行命令1:mysql -u usernae -p password命令2:mysql -u username -p password -h 需要连接的mysql主机名(localhost本地主机名)或是mysql的ip地址(默认为:127.0.0.1)-P 端口号(默认:3306端口)使用其中任意一个就OK,输入命令后DOS命令框得到mysql>就说明已经进入了mysql系统2. 查看mysql当中的._mysql -u user