unhandled exception type Exception?是什么问题

2025-04-13 00:53:50
推荐回答(5个)
回答1:

请问是否是编译期出的问题,不是catch的问题这个错误是指:你有一个方法会抛出异常,但是你没有捕捉。程序改成如下就好了: public class Example8_4 {
public static void main(String[] args) {
try {
method();
} catch (Exception e) {
e.printStackTrace();
}
}

static void method()throws Exception {
try {
System.out.println("try:performed");
} finally {
System.out.println("finally:always performed");
}
}
}

回答2:

你的method定义抛出Exception异常,但是在main里面却没有相关处理的功能
所以才会提示说 unhandled exception type
试著把main里面的method加上个try catch
如下:

public static void main(String args[]){
try{
method(); //这一行提示unhandled exception type Exception
} catch (Exception ex) {
//加入处理错误程式码
}
}

回答3:

在method()方法中声明抛出异常,但是却没有创建异常对象,所以JVM不知道要抛出的什么类型的对象

回答4:

Ctrl+1 自动补充抛出异常 public static void main(String[] args) throws ParseException

回答5:

缺少 }catch(Exception ex){ throws Exception {}
这是一个抛异常的方法 你有用try 但又不抛异常 所以报错!