I have a problem in java, as we know if there is some code error due to some exceptions, our code will not able to run if we are not setting up try-catch block, but sometimes, we don't know which line make problem, for example, this one
@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");
}
I want to know all results c, d, e, f, g, and h
but as you know variables e
and g
might have possibility get an error because divided by 0, and the program will exit at that time ( in e or g )
is there any way for running the rest of the code, when we execute variable e and get errors without exiting the program immediately and without writing try-catch
blocks?
Thank you
CodePudding user response:
Test for user input(?)
add an if statement before the '/' operators.
also, try-catch is there EXACTLY for, well, catching this sort of errors.
If you can specify why you cant use them,
perhaps it will help find a way around the issue.
(its like saying you need "a whole number" but cannot use "integer")
Tl;dr -> just add an (b!=0) "if" statement before your "e=a/b"