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);
}
}

最新文章

  1. gRPC源码分析2-Server的建立
  2. Java中的两个关键字——super、this
  3. Memcache入门知识
  4. linux下出现java.net.UnknownHostException
  5. FFmpeg的HEVC解码器源代码简单分析:解析器(Parser)部分
  6. java线程池与五种常用线程池策略使用与解析
  7. C#学习-扩展方法
  8. 查看crontab运行状态
  9. C# 中ref与out关键字区别
  10. Compiling U-Boot
  11. 潭州课堂25班:Ph201805201 tornado 项目 第四课 增加用户注册登录(课堂笔记)
  12. 点击事件click和onclick的区别
  13. BZOJ 3744 Gty的妹子序列 (分块 + BIT)
  14. Json To CSharp
  15. es 中的 Iterator
  16. Python爬虫实战---抓取图书馆借阅信息
  17. php中将数组转换为指定符号分割的字符串
  18. 读书笔记 Week7 2018-4-24
  19. 在浏览器中输入URL后,执行的全部过程。(一次完整的http请求过程)
  20. Luogu3936 Coloring(模拟退火)

热门文章

  1. shell脚本自动化安装pgsql10.5版本
  2. 2019.12.09 java循环(do……while)
  3. let
  4. 特征的非线性变换(Feature Non-linear Transformation)
  5. SpringBoot:关于默认连接池Hikari的源码剖析
  6. el-table里面的列需要对比两个返回参数
  7. ubuntu之路——day12.1 不用tf和torch 只用python的numpy在较为底层的阶段实现简单神经网络
  8. CSS背景和精灵图
  9. vue+elementui搭建后台管理界面(8 同步/异步获取数据渲染table)
  10. 从零开始搭建实验室Ubuntu服务器 | 深度学习工作站