No exception of type Exception can be thrown

No exception of type Exception can be thrown; an exception type must be a subclass of Throwable Exception.java

翻译:不能引发异常类型的异常;异常类型必须是throwable exception.java的字类.

package com.dream.Ex;
/**
 * 
 * 2022年2月26日
 * @company
 * @author dream
 * @description 异常处理
 */
public class Exception1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		div(10, 2);
	}
	
	/**
	 * 除法运算
	 */
	private static void div(int num1,int num2){
		int arr[]= {1,2,3,4,5};
		try {
			int result=num1/num2;
			//alt+/
			System.out.println("result="+result);
			System.out.println(arr[1]);
			System.out.println(arr.length);
		}catch (ArithmeticException e) {
			System.out.println("除数不能为0!");
		}catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("数组下标越界!");
		}catch(NullPointerException e) {
			System.out.println("空指针异常!");
		}catch(Exception e) {
			System.out.println("数据出现异常错误!");
		}
		System.out.println("程序结束!");
	}
}

中catch(Exception e)出现错误:No exception of type Exception can be thrown; an exception type must be a subclass of Throwable Exception.java

解决方法:将Eclipse中默认的Jre更换为你配置环境的JDK,再重新新建项目即可.
步骤照片如下:
Windows->Perferences->Java->Installed JRES->ADD->Standard VM->Next->Directory->jdk1.8.0_134(jdk版本)->选择文件夹->Apply->重新新建项目即可.

在这里插入图片描述
在这里插入图片描述
点击"Add"后,
在这里插入图片描述
点击"Next>"后:
在这里插入图片描述
点击Directory后:
在这里插入图片描述
选择JDK后,即可.
将JRE更换成JDK版本.
在这里插入图片描述
点击"Apply"即可.
在重新新建项目,再用

try{
//代码块即可
}
catch(Exception e){
//异常处理
}

就不会出现问题了 .

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值