一篇文章看懂Java异常处理
作者:weixin_51178129 时间:2023-10-07 12:08:37
异常的定义
在java中,异常就是java在编译、运行或运行过程中出现的错误
总共有三种:1.编译错误 2.运行错误 3.逻辑错误
1.编译错误是因为程序没有遵循语法规则,编译程序能够自己发现并且提示我们错误的原因和位置,这个也是新手在刚接触编程语言时经常遇到的问题。
2.运行时错误是因为程序在执行时,运行环境发现了不能执行的操作。
3.逻辑错误是因为程序没有按照预期的逻辑顺序执行。异常也就是指程序运行时发生错误,而异常处理就是对这些错误进行处理和控制。
package org.oracle.test;
public class TryDemo9 {
public static void main(String[] args) {
System.out.println(1/0);//0不能做除数
System.out.println("你好");//程序捕获异常,程序终止,不执行
}
}
在代码执行过程中遇到异常就会终止
运行的结果里会告诉我们异常的类型,异常出现的位置
异常的分类
RuntimeException:运行时异常,一般不手动处理,出问题了再处理。
其他Exception:必须要经过手动处理。
Error:一般指的是系统级错误。
异常的处理方法
try…catch处理
package org.oracle.test;
import java.util.Scanner;
public class TryDemo2 {
public static void main(String[] args) {
System.out.println(testOne());
}
/*
* 当try、catch、finally代码块中都有return语句,
* 程序无论报错与否都会执行finally里面的renturn语句
*/
public static double testOne() {
try {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个数字:");
int a = sc.nextInt();
System.out.println("请输入第二个数字:");
int b = sc.nextInt();
int result = a / b;
//System.out.println("result:" + result);
System.out.println("执行了try块");
return result;
}catch (Exception e) {
e.printStackTrace();
return 0;
}finally {
return -1000;
}
}
}
运行结果:
try-- 用于监听。将要被监听的代码(可能抛出异常的代码)放在try语句块之内,当try语句块内发生异常时,异常就被抛出。
catch-- 用于捕获异常。catch用来捕获try语句块中发生的异常。
finally-- finally语句块总是会被执行。它主要用于回收在try块里打开的物力资源(如数据库连接、网络连接和磁盘文件)。
throw 和throws
throws表示方法准备要扔出来一个异常
throw表示向外抛出异常
package org.oracle.test;
import java.util.InputMismatchException;
public class TryDemo8 {
public static void main(String[] args) {
try {
testThree();
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
public static void testOne() throws InputMismatchException{
throw new InputMismatchException();
}
public static void testTwo() throws Exception {
try {
testOne();
} catch (InputMismatchException e) {
throw new Exception("新异常1",e);
}
}
public static void testThree() throws Exception {
try {
testTwo();
} catch (Exception e) {
Exception e1 = new Exception("新异常2");
e1.initCause(e);
throw e1;
}
}
}
运行结果:
自定义异常
自定义异常:直接继承Exception或者RuntimeException来是实现自定义异常
package org.oracle.test;
import java.util.Scanner;
public class TryDemo6 {
public static void main(String[] args){
try {
hotelRule();
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* 基帝开了个情侣酒店,基帝酒店规矩:18岁以下和80岁上以上客户,需要家人陪同入住
*/
public static void hotelRule() throws Exception {
Scanner sc = new Scanner(System.in);
System.out.println("请输入客户年龄:");
int age = sc.nextInt();
if(age < 18 || age > 80) {
throw new HotelException();
}else {
System.out.println("成功入住");
}
}
}
package org.oracle.test;
/*
* 自定义异常需继承Exception
*/
public class HotelException extends Exception{
public HotelException() {
super("由于年龄问题,无法入住");
}
}
运行结果:
来源:https://blog.csdn.net/weixin_51178129/article/details/121385023
标签:java,异常,处理
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Intellij IDEA创建spring-boot项目的图文教程
2022-06-15 01:09:59
![](https://img.aspxhome.com/file/2023/0/81920_0s.png)
springboot配置文件中属性变量引用方式@@解读
2023-11-24 20:39:18
![](https://img.aspxhome.com/file/2023/6/59506_0s.png)
使用HandlerMethodArgumentResolver用于统一获取当前登录用户
2023-04-20 12:09:16
Android使用友盟集成QQ、微信、微博等第三方分享与登录方法详解
2022-03-07 01:46:30
![](https://img.aspxhome.com/file/2023/6/137966_0s.png)
C#实现身份证验证功能的示例代码
2022-05-14 20:53:25
![](https://img.aspxhome.com/file/2023/6/106516_0s.png)
Android 按指定大小读取图片的实例
2022-06-16 18:26:25
Android App调试内存泄露之Cursor篇
2023-11-22 03:36:29
使用C# CefSharp Python采集某网站简历并且自动发送邀请短信的方法
2023-11-17 14:09:34
![](https://img.aspxhome.com/file/2023/6/102196_0s.png)
Java反射机制基础详解
2023-07-17 04:36:29
![](https://img.aspxhome.com/file/2023/2/78022_0s.png)
Android跳转三方应用实例代码
2022-02-01 11:15:42
![](https://img.aspxhome.com/file/2023/7/125407_0s.jpg)
C#中的图像Image类与打印Printing类用法
2022-07-25 06:24:36
Spring Boot接口幂等插件用法示例解析
2022-04-29 16:47:11
![](https://img.aspxhome.com/file/2023/0/78890_0s.png)
Android onClick方法与setOnClickListener方法对比
2022-02-09 22:21:40
Android编程中出现The connection to adb is down问题的解决方法
2022-06-27 17:46:07
SpringBoot+Mybatis项目使用Redis做Mybatis的二级缓存的方法
2021-06-17 20:09:50
Android viewpager 3D画廊的实现方法
2021-05-28 21:19:27
浅谈java中==以及equals方法的用法
2021-09-29 15:21:45
Android Studio实现简单计算器功能
2023-10-17 03:54:42
![](https://img.aspxhome.com/file/2023/0/89530_0s.jpg)
Java实现字符串解析为日期时间的方法示例
2021-10-29 17:43:34
![](https://img.aspxhome.com/file/2023/8/91148_0s.jpg)
Android TextView中文字通过SpannableString设置属性用法示例
2023-07-26 07:11:51
![](https://img.aspxhome.com/file/2023/8/84748_0s.jpg)