I am trying to handle a divide by zero exception manually in python3.5, however, it keeps ignoring my user defined case and giving the default exception. How can I avoid this? Any help is appreciated.
NOTE: I'm using a list as a stack, hence the last and is checking whether the top elem is 0. Also, the order is correct - I deliberately take the top elem as denominator
elif 'div' in command:
if len(stack)!=0 and len(stack)!=1 and is_digit(stack[len(stack)-2]) and is_digit(stack[len(stack)-1]) and stack[len(stack)-1]!=0:
op2 = stack.pop();
op1 = stack.pop();
stack.append(str( int(int(op1) / int(op2)) ) + '\n');
else:
stack.append(':error:\n');
This gives ZeroDivisionError: division by zero INSTEAD of :error: