当前位置: 代码网 > it编程>编程语言>Java > Java异常处理是什么?常见异常类型及解决方法详解

Java异常处理是什么?常见异常类型及解决方法详解

2026年08月25日 Java 我要评论
异常情况异常指的是在编译阶段没有语法错误的代码,在程序运行过程中的产生的错误,常见的异常情况:程序执行所要打开的文件不存在所加载的文件找不到或者格式不对访问数组时,超出数组元素的索引范围数学运算时,除

异常情况

异常指的是在编译阶段没有语法错误的代码,在程序运行过程中的产生的错误,常见的异常情况:

  • 程序执行所要打开的文件不存在
  • 所加载的文件找不到或者格式不对
  • 访问数组时,超出数组元素的索引范围
  • 数学运算时,除数为0

实例

//java之异常:指的是在编译阶段没有语法错误的代码,在程序运行过程中的产生的错误
public class text {

	public static void main(string[] args) {
		
		int[] array = new int[10];
		array[10] = 300;  //超出数组边界产生的异常
		
		//发生异常后,若异常没有进行处理就会中断程序的执行
		system.out.println("haozige");

	}
}

java的异常类

java中所有的异常类都是系统类throwable的子类。

常用的异常类

  • arithmeticexception:算术运算错误,例如除数除以0
  • arrayindexoutofboundsexception:数组下标超出范围
  • arraystoreexception:数组类型与赋值给数组的数据类型不兼容
  • classcastexception:无效的转换
  • illegalargumentexception :调用方法时的参数不符合规定
  • illegalstateexception: 应用程序与环境的状态错误
  • illegalthreadstateexception:要求的操作与目前的thread状态不兼容
  • negativearraysizeexception:创建数组的大小是负数
  • numberformatexception:无效的字符串转换成数字格式
  • stringindexoutofbounds:字符串的索引超出范围
  • nosuchelementexception:集合中已无元素但仍读取 

异常处理

监测可能发生的一种异常

try {
        //需要监测可能发生异常的语句
}
catch (异常类 变量名) {
        //异常发生时的执行语句
}
  • 异常类需要和需要检测可能发生异常的语句的异常类型相同 

实例

//java之异常:指的是在编译阶段没有语法错误的代码,在程序运行过程中的产生的错误
public class text {

	public static void main(string[] args) {
		
		int[] array = new int[10];
		
		try {  //使用try语句监测异常
			array[10] = 300;  //超出数组边界的异常
		}catch (arrayindexoutofboundsexception e) {  //使用catch语句处理异常
			system.out.println("异常内容:" + e.tostring());
		}
		
		//使用try...catch语句处理异常后,程序不会被中止
		system.out.println("haozige");  
	}
}

监测可能发生的多种异常

try {
        //需要监测可能发生异常的语句
}
catch (异常类 变量名) {
        //异常发生时的执行语句
}
catch (异常类 变量名) {
        //异常发生时的执行语句
 }
.
.
.
catch (异常类 变量名) {
        //异常发生时的执行语句
}
  • 当在try语句中监测多种异常时,当第一个被监测到的异常发生时,符号同类型的catch语句就会被执行,而其他catch语句将会被跳过。
  • 由于java的异常类都是继承与exception类,因此对于不确定异常类型可以使用exception类来捕获
  • 捕获不确定异常的catch语句一定要写在所有catch语句的最后

实例

数组越界的异常被处理

//java之异常:指的是在编译阶段没有语法错误的代码,在程序运行过程中的产生的错误
public class text {

	public static void main(string[] args) {
		
		int[] array = new int[10];
		
		try {  //使用try语句监测异常
			
			array[10] = 300;  //超出数组边界的异常
			int tmp = 20/0;  //被除数是0的异常
			int n = integer.parseint("你好");  //不确定类型的异常
			
		}catch (arrayindexoutofboundsexception e) {  //使用catch语句处理异常
		
			system.out.println("异常内容:" + e.tostring());
			system.out.println("数组越界的异常被处理");
		
		}catch (arithmeticexception e) {
			
			system.out.println("异常内容:" + e.tostring());
			system.out.println("被除数为0的异常被处理");
			
		}catch (exception e) {  //使用exception类捕获不确定异常
			
			system.out.println("异常内容:" + e.tostring());
			system.out.println("不确定类型的异常被处理");
		
		}
		
		//使用try...catch语句处理异常后,程序不会被中止
		system.out.println("haozige");  
	}
}

被除数是0的异常被处理 

//java之异常:指的是在编译阶段没有语法错误的代码,在程序运行过程中的产生的错误
public class text {

	public static void main(string[] args) {
		
		int[] array = new int[10];
		
		try {  //使用try语句监测异常
			
			array[9] = 300;  //超出数组边界的异常
			int tmp = 20/0;  //被除数是0的异常
			int n = integer.parseint("你好");  //不确定类型的异常
			
		}catch (arrayindexoutofboundsexception e) {  //使用catch语句处理异常
		
			system.out.println("异常内容:" + e.tostring());
			system.out.println("数组越界的异常被处理");
		
		}catch (arithmeticexception e) {
			
			system.out.println("异常内容:" + e.tostring());
			system.out.println("被除数为0的异常被处理");
			
		}catch (exception e) {  //使用exception类捕获不确定异常
			
			system.out.println("异常内容:" + e.tostring());
			system.out.println("不确定类型的异常被处理");
		
		}
		
		//使用try...catch语句处理异常后,程序不会被中止
		system.out.println("haozige");  
	}
}

不确定类型的异常被处理

//java之异常:指的是在编译阶段没有语法错误的代码,在程序运行过程中的产生的错误
public class text {

	public static void main(string[] args) {
		
		int[] array = new int[10];
		
		try {  //使用try语句监测异常
			
			array[9] = 300;  //超出数组边界的异常
			int tmp = 20/5;  //被除数是0的异常
			int n = integer.parseint("你好");  //不确定类型的异常
			
		}catch (arrayindexoutofboundsexception e) {  //使用catch语句处理异常
		
			system.out.println("异常内容:" + e.tostring());
			system.out.println("数组越界的异常被处理");
		
		}catch (arithmeticexception e) {
			
			system.out.println("异常内容:" + e.tostring());
			system.out.println("被除数为0的异常被处理");
			
		}catch (exception e) {  //使用exception类捕获不确定异常
			
			system.out.println("异常内容:" + e.tostring());
			system.out.println("不确定类型的异常被处理");
		
		}
		
		//使用try...catch语句处理异常后,程序不会被中止
		system.out.println("haozige");  
	}
}

finally语句

try {
        //需要监测可能发生异常的语句
}
catch (异常类 变量名) {
        //异常发生时的执行语句
}
catch (异常类 变量名) {
        //异常发生时的执行语句
}
.
.
.
catch (异常类 变量名) {
        //异常发生时的执行语句
}
finally {
        //不管被监测的语句是否发生异常都会执行的语句
}

数组越界的异常被处理,finally语句同样执行

实例

//java之异常:指的是在编译阶段没有语法错误的代码,在程序运行过程中的产生的错误
public class text {

	public static void main(string[] args) {
		
		int[] array = new int[10];
		
		try {  //使用try语句监测异常
			
			array[10] = 300;  //超出数组边界的异常
			int tmp = 20/0;  //被除数是0的异常
			int n = integer.parseint("你好");  //不确定类型的异常
			
		}catch (arrayindexoutofboundsexception e) {  //使用catch语句处理异常
		
			system.out.println("异常内容:" + e.tostring());
			system.out.println("数组越界的异常被处理");
		
		}catch (arithmeticexception e) {
			
			system.out.println("异常内容:" + e.tostring());
			system.out.println("被除数为0的异常被处理");
			
		}catch (exception e) {  //使用exception类捕获不确定异常
			
			system.out.println("异常内容:" + e.tostring());
			system.out.println("不确定类型的异常被处理");
		
		}finally {
			system.out.println("对于finally语句,不管被监测的语句是否发生异常都会执行的语句");
		}
		
		//使用try...catch语句处理异常后,程序不会被中止
		system.out.println("haozige");  
	}
}

没有异常发生,finally语句同样执行 

//java之异常:指的是在编译阶段没有语法错误的代码,在程序运行过程中的产生的错误
public class text {

	public static void main(string[] args) {
		
		int[] array = new int[10];
		
		try {  //使用try语句监测异常
			
			array[9] = 300;  //超出数组边界的异常
			int tmp = 20/5;  //被除数是0的异常
			//int n = integer.parseint("你好");  //不确定类型的异常
			
		}catch (arrayindexoutofboundsexception e) {  //使用catch语句处理异常
		
			system.out.println("异常内容:" + e.tostring());
			system.out.println("数组越界的异常被处理");
		
		}catch (arithmeticexception e) {
			
			system.out.println("异常内容:" + e.tostring());
			system.out.println("被除数为0的异常被处理");
			
		}catch (exception e) {  //使用exception类捕获不确定异常
			
			system.out.println("异常内容:" + e.tostring());
			system.out.println("不确定类型的异常被处理");
		
		}finally {
			system.out.println("对于finally语句,不管被监测的语句是否发生异常都会执行的语句");
		}
		
		//使用try...catch语句处理异常后,程序不会被中止
		system.out.println("haozige");  
	}
}

方法的异常处理

异常发生在所调用的方法中时,方法内有监测和处理异常的语句

实例

public class text {
	
	static int[] array = new int[10];
	
	static void initarray(){
		try{
			array[10] = 66;
		}catch (arrayindexoutofboundsexception e){
			system.out.println("异常情况:" + e.tostring());
		}finally {
			system.out.println("haozige");
		}
	}
	
	public static void main(string[] args) {
		
		initarray();
		
	}
}

 异常发生在所调用的方法中时,方法内没有监测和处理异常的语句,那么系统就会自动到它的上一层方法中寻找符号的catch语句

实例

public class text {
	
	static int[] array = new int[10];
	
	static void initarray(){
		array[10] = 66;
	}
	
	public static void main(string[] args) {
		try{
			initarray();  //方法内没有处理异常的语句,自动到main方法中寻找符号的catch语句
		}
		catch (arrayindexoutofboundsexception e){
			system.out.println("异常情况:" + e.tostring());
		}finally {
			system.out.println("haozige");
		}
	}
}

手动抛出异常

当程序在运行时发生了某种异常,默认情况异常会被jvm拦截并抛出。但有时会因为发生某种错误,导致程序无法继续下去,即异常无法自动抛出,此时需要我们手动抛出异常。

使用throw语句抛出异常

  • 因为所有的异常类都是throwable的子类,因此使用throw语句抛出异常对象时,这个异常对象必须是throwable类或者throwable子类的对象。
  • 当使用throw语句抛出异常后,由catch语句捕获所符合的异常,在throw之后的语句将不会执行。
try {
        //需要监测可能发生异常的语句
        throw new  要抛出的异常对象
}
catch (异常类 变量名) {
        //异常发生时的执行语句
}

实例

public class text {
	
	static void myprintf(int age){
		try{
			if(age < 20){
				throw new exception("未够年龄");
			}
		}catch (exception e){
			system.out.println("异常内容:" + e.tostring());
		}
	}
	
	public static void main(string[] args) {
	
		myprintf(18);
		
		system.out.println("done!");
	}
}

使用throws语句声明某个方法有可能发生的异常类型

声明xxx方法可能会发生yyy1,yy2,...异常

void xxx() throws yyy1,yy2 ,... {}

实例

public class text {
	
	static void myprintf(int age) throws exception{  //声明该方法可能会发生exception异常,该异常不一定会出现
		system.out.println("haozige");
		if (age < 20){
			throw new exception();
		} 
	}
	
	public static void main(string[] args) {
	
		try {
			myprintf(10);  //调用该方法时,使用异常类捕获这种可能出现的异常
		} catch (exception e) {
			system.out.println("jiangxiaoya");
		}
		
		system.out.println("done!");
	}
}

throw语句和throws语句的区别

  •  throw语句是手动抛出异常,该异常是一定会发生的,一定要进行异常捕获。
  • throws语句是声明某个方法有可能发生某种类型的异常,提醒调用该方法的调用者应该进行这种类型的异常捕获,该异常不一定会发生。

自定义异常类

对于某些特殊的异常时,java类库提供的异常类无法满足我们的需求,因此我们需要自定义的异常类来处理这些特殊的异常。

使用继承类的方式自定义异常类

  • 自定义的异常类需要继承exception类或者exception类衍生出来的子类。
  • exception类没有定义属于自己的方法,因此可以重写throwable类(exception类的父类)的方法来定义自己所需要的异常信息。

throwable类常用的方法成员

  • string getlocalizedmessage(){}:获得异常的局部说明信息 
  • string getmessage(){}:获得异常信息
  • string tostring(){}: 获得包含异常语句的字符串 
  • throwable getcause(){}:获得造成目前异常的异常对象 
  • throwable fillinstacktrace(){}:获得完整堆栈追踪的异常对象          
  • void printstacktrace()(){}:显示堆栈追踪 

使用throw语句手动抛出自定义异常类的异常对象

class 自定义异常类 extends 异常类 {
        //程序语句
} 
.
.
.
throw 自定义异常类对象
.
.
.

实例

class myexception extends exception {  //自定义异常类继承exception类
	
	private string name;
	
	myexception(string name){  //构造方法对数据成员赋值
		this.name = name;
	}
	
	public string tostring(){  //对exception类的tostring()进行方法重写
		return "发生了myexception异常";
	}
	
	public string getmessage(){  //对exception类的getmessage()进行方法重写
		return "怎么回事";
	}
	
	public void myprintf(){  //自定义异常类myexception的自定义方法
		system.out.println(name);
	}
}


public class text {
	
	public static void main(string[] args) {
	
		try{
			throw new myexception("jiangxiaoya");  //手动抛出自定义异常类对象
		}catch (myexception m){
			system.out.println(m.tostring());
			system.out.println(m.getmessage());
			m.myprintf();
		}
		
	}
}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com