d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > What's The Difference? (java Catch Question)
Add Reply New Topic New Poll
Member
Posts: 31,293
Joined: Mar 25 2009
Gold: 0.00
Oct 18 2019 07:04pm
Code
public static void main(String[] args) {

try {
int result = divide();
System.out.println(result);
} catch (ArithmeticException | NoSuchElementException e) {
System.out.println(e.toString());
System.out.println("Unable to perform division, autopilot shutting down");
}


}



private static int divide() {
int x, y;
// try {
x = getInt();
y = getInt();
System.out.println("x is " + x + ", y is " + y);
return x / y;
// } catch (NoSuchElementException e) {
// throw new ArithmeticException("no suitable input");
// } catch (ArithmeticException e) {
// throw new ArithmeticException("attempt to divide by zero");
// }

}









what's the difference between the 2 catch statements in divide method, and
Code
catch (ArithmeticException | NoSuchElementException e)




They both seem to do 2 catches, to see which ones to use....
Member
Posts: 22,237
Joined: Dec 6 2008
Gold: 2,971.74
Trader: Mediator
Oct 18 2019 10:51pm
They are both doing two catches.
The divide catches gives a more direct output error than you would get with the second catch offered with less code.

i.e for Divide
If you get a NoSuchElement error, it'll strictly go into that catch and print out that direct error (but you should probably print 'e' in your throw as well)
If you get a ArithmeticFunction error, itl'l strictly deal with arithmetic.

i.e for second:
If it gets either exception, it'll toss it to e. In this case, you could print a general throw, such as "no suitable input" and it would handle both types of exceptions, but it is less versatile.
You can make it do the same thing as the divide fx by creating an if statement for checking the instance type (NoSuch or Arithmetic), but at that point you should really just go back to using the catch offered in divide.

I wrote up a quick mock-up to help comprehend, it's in Python but you should get the gist.

Code
foo = None
bar = 0

try:
a = foo//0
except TypeError as e:
print("foo is wrong variable type!\nERROR: ", str(e))

try:
b = (bar // 0)
except ZeroDivisionError as e:
print("Cant divide by 0!\nERROR: ", str(e))


try:
a = foo//0
b = (bar // 0)
except TypeError or ZeroDivisionError as e:
print("ERROR!")
print("ERROR IS ZERODIVISION?: ", isinstance(e, ZeroDivisionError))
print("ERROR IS TYPEERROR?: ", isinstance(e, TypeError))


Logs:
Code
foo is wrong variable type!
ERROR: unsupported operand type(s) for //: 'NoneType' and 'int'
Cant divide by 0!
ERROR: integer division or modulo by zero
ERROR!
ERROR IS ZERODIVISION?: False
ERROR IS TYPEERROR?: True


This post was edited by Candyzcanes on Oct 18 2019 11:00pm
Member
Posts: 31,293
Joined: Mar 25 2009
Gold: 0.00
Oct 18 2019 11:10pm
hmmm thanks

what did you mean by this:

Quote
(but you should probably print 'e' in your throw as well)
Member
Posts: 22,237
Joined: Dec 6 2008
Gold: 2,971.74
Trader: Mediator
Oct 18 2019 11:16pm
Quote (ferf @ Oct 18 2019 11:10pm)
hmmm thanks

what did you mean by this:


Printing your actual error. You're currently just posting specific cases like:
throw new ArithmeticException("no suitable input");
throw new ArithmeticException("attempt to divide by zero");

ArithmeticException is a general exception to some extent. So, at least during testing phases, you should print what the actual error is so you can make sure you know what's actually going on.
My code for that was:
print("Cant divide by 0!\nERROR: ", str(e))

(I concatenate the string that is more relevant to me and the exception)
Member
Posts: 31,293
Joined: Mar 25 2009
Gold: 0.00
Oct 19 2019 12:28am
Quote (Candyzcanes @ Oct 19 2019 01:16am)
Printing your actual error. You're currently just posting specific cases like:
throw new ArithmeticException("no suitable input");
throw new ArithmeticException("attempt to divide by zero");

ArithmeticException is a general exception to some extent. So, at least during testing phases, you should print what the actual error is so you can make sure you know what's actually going on.
My code for that was:
print("Cant divide by 0!\nERROR: ", str(e))

(I concatenate the string that is more relevant to me and the exception)


Gotchya thanks :)
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll