java的错误分类
2024-10-21 09:40:19
java的错误分类
java中的错误分为两大类:Error和Exception错误。
Error 是程序无法处理的错误,表示运行应用程序中较严重问题,修改程序本身是不能解决的。例如java运行时产生的系统内部错误(虚拟机错误),资源耗尽导致的错误。
Exception是异常类,它表示程序本身可以处理的错误。主要分为非运行异常和运行异常。
1.非运行时异常(编译异常)
IOException异常
表示由失败或中断的I / O操作产生的异常类。
SQLException异常
表示访问数据库错误或者数据库信息错误。
2.运行时异常(RuntimeException)
常见的运行时异常:
ClassCastException(类转换异常)
public class Demo{
public static void main(String[] args){
Object x = new Integer(0);
System.out.println((String)x);
}
}
StringIndexOutOfBoundsException(字符串角标越界异常)
public class Demo {
public static void main(String[] args){
String s = "abcdefg";
System.out.print(s.charAt(9));
}
}
ArrayIndexOutOfBoundsException(数组角标越界异常)
public class Demo {
public static void main(String[] args){
int [] arr = new int[4];
System.out.println("arr[0]="+arr[4]);
}
}
NullPointerException(空指针异常)
public class HelloWorld{
public static void main(String[] args){
int [] arr = new int[4];
arr[0] = 5;
System.out.println("arr[0]="+arr[0]);
arr = null;
System.out.println("arr[0]="+arr[0]);//报错
}
}
ArrayStoreException(数据存储异常,操作数组时类型不一致)
public class Test{
public static void main(String[] args){
Object x[] = new String[3];
x[0] = new Integer(0);
System.out.println(x[0]);
}
}
ArithmeticException(算术异常)
public class FindError {
public static void main(String[] args){
int x = 4,y = 0;
int result = x / y;
System.out.print(result);
}
}
最新文章
- gRPC源码分析2-Server的建立
- Java中的两个关键字——super、this
- Memcache入门知识
- linux下出现java.net.UnknownHostException
- FFmpeg的HEVC解码器源代码简单分析:解析器(Parser)部分
- java线程池与五种常用线程池策略使用与解析
- C#学习-扩展方法
- 查看crontab运行状态
- C# 中ref与out关键字区别
- Compiling U-Boot
- 潭州课堂25班:Ph201805201 tornado 项目 第四课 增加用户注册登录(课堂笔记)
- 点击事件click和onclick的区别
- BZOJ 3744 Gty的妹子序列 (分块 + BIT)
- Json To CSharp
- es 中的 Iterator
- Python爬虫实战---抓取图书馆借阅信息
- php中将数组转换为指定符号分割的字符串
- 读书笔记 Week7 2018-4-24
- 在浏览器中输入URL后,执行的全部过程。(一次完整的http请求过程)
- Luogu3936 Coloring(模拟退火)
热门文章
- shell脚本自动化安装pgsql10.5版本
- 2019.12.09 java循环(do……while)
- let
- 特征的非线性变换(Feature Non-linear Transformation)
- SpringBoot:关于默认连接池Hikari的源码剖析
- el-table里面的列需要对比两个返回参数
- ubuntu之路——day12.1 不用tf和torch 只用python的numpy在较为底层的阶段实现简单神经网络
- CSS背景和精灵图
- vue+elementui搭建后台管理界面(8 同步/异步获取数据渲染table)
- 从零开始搭建实验室Ubuntu服务器 | 深度学习工作站