1.需求
需求:在做数据导入的时候,因为数据量较大,有时候数据会有问题,会抛出异常,但这个异常很难定位到某一行数据。
这时候需要做处理,把他定位到某一行。把数据的信息放到异常信息里面。
面临的问题:
当前代码情况,在控制层中捕获异常并反馈给前端,但是着时候捕获不到数据出问题的具体位置。
2.实验
1.需求 (根据源码补个简单的需求)B类中抛出异常,给A类的函数,A类的函数把异常信息返回给主函数,并显示出来
2.代码
package testJava3;
public class ExceptionTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("异常测试");
ExceptionTest ecpt= new ExceptionTest();
ExceptionTest.A a = ecpt.new A();
String str = a.fun();
System.out.println(str);
}
class A {
String fun() {
try {
B b = new B();
int a = b.fun();
return "成功";
} catch (Exception e) {
return "校验:"+e.getMessage();
}
}
}
class B{
int fun() throws Exception {
int a = 0;
int b = 0;
int c = 1;
try {
a = c / b;
}catch(Exception e) {
throw new Exception("异常:"+c+"/"+b+e.getMessage());
}
return a;
}
}
}
3.运行效果
异常测试
校验:异常:1/0/ by zero
本文介绍了一个具体的Java异常处理案例,通过在不同类间传递异常信息,最终将详细的错误信息反馈给用户。文章详细展示了如何在B类中抛出异常,并在A类中捕获这一异常,进而将异常的具体细节打印出来。
2043

被折叠的 条评论
为什么被折叠?



