在 Java 中出现异常后,有什么方法可以让我在下一行继续?
Is there any method that i can continue on the next line after getting exceptions in Java?
我在 java 中遇到问题,因为我们知道如果由于某些异常而导致代码错误,如果我们没有设置 try-catch,我们的代码将无法 运行块,但有时,我们不知道是哪一行出问题,例如,这一行
@Test
public void testExceptions(int a , int b) {
GlobalExceptionHandler globalExceptionHandler = new GlobalExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(globalExceptionHandler);
System.out.println("Error ");
Integer c = a + b;
Integer d = a * b;
Integer e = a / b;
Integer f = a - b;
Integer g = f / c;
Integer h = g * c;
System.out.println("this is new Line");
}
我想知道所有结果 c, d, e, f, g, and h
但正如你所知,变量 e
和 g
可能会出错,因为除以 0,程序将在那个时候退出时间 ( in e or g )
当我们执行变量 e 并在不立即退出程序且不写入 try-catch
块的情况下出现错误时,有什么方法可以 运行 处理其余代码?
谢谢
测试用户输入(?)
在“/”运算符之前添加一个 if 语句。
此外,try-catch 正是为了捕获此类错误。
如果你能说明为什么你不能使用它们,
也许这将有助于找到解决问题的方法。
(就像说你需要“整数”但不能使用“整数”)
Tl;dr -> 只需在“e=a/b”
之前添加 (b!=0) “if” 语句
我在 java 中遇到问题,因为我们知道如果由于某些异常而导致代码错误,如果我们没有设置 try-catch,我们的代码将无法 运行块,但有时,我们不知道是哪一行出问题,例如,这一行
@Test
public void testExceptions(int a , int b) {
GlobalExceptionHandler globalExceptionHandler = new GlobalExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(globalExceptionHandler);
System.out.println("Error ");
Integer c = a + b;
Integer d = a * b;
Integer e = a / b;
Integer f = a - b;
Integer g = f / c;
Integer h = g * c;
System.out.println("this is new Line");
}
我想知道所有结果 c, d, e, f, g, and h
但正如你所知,变量 e
和 g
可能会出错,因为除以 0,程序将在那个时候退出时间 ( in e or g )
当我们执行变量 e 并在不立即退出程序且不写入 try-catch
块的情况下出现错误时,有什么方法可以 运行 处理其余代码?
谢谢
测试用户输入(?)
在“/”运算符之前添加一个 if 语句。
此外,try-catch 正是为了捕获此类错误。
如果你能说明为什么你不能使用它们,
也许这将有助于找到解决问题的方法。
(就像说你需要“整数”但不能使用“整数”)
Tl;dr -> 只需在“e=a/b”