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){
//异常处理
}
就不会出现问题了 .