Situation: you need to get the type byte after the operation int/int . If the result is in the byte range, then output it, if not, then output an error message. And also handle Exception when dividing by 0 and Exception in principle. How to make such a piece of code work? Tried in line 3 through byte tempResult = (byte) (operand1 / operand2); but in the end received for the last call is not what you need.
public byte divisionByte(int operand1, int operand2) { try { byte tempResult = operand1 / operand2; if (tempResult >= 127 && tempResult <= -128) { System.out.println("Результат не входит в диапазон Byte."); } else System.out.println(operand1 + " / " + operand2 + " = " + tempResult); return tempResult; } catch (ArithmeticException e) { System.out.println(operand1 + " / " + operand2 + " = We can't divide by 0. " + e + ". Shame on you!"); return 1; } catch (Exception e) { System.out.println("ERROR " + e); return 1; } } In the Main class, one call should give a value, the second error message when dividing by 0 , the third that "Результат не входит в диапазон Byte"
calc.divisionByte(2, 2); calc.divisionByte(2, 0); calc.divisionByte(1000, 2);