Skip to content
Snippets Groups Projects

Grx displays error message instead of crashing

1 file
+ 23
1
Compare changes
  • Side-by-side
  • Inline
@@ -17,6 +17,17 @@ class Base(Enum):
HEXADECIMAL = 4
class ValueToBig(Exception):
"""
A class representing the error of a user inputting a value that does not fit in the memory.
Used to differentiate between wrong input using the exception python raises by default (ValueError)
and the error of a value that does not fit.
"""
pass
class IntegerMemoryWindow(MemoryWindow):
"""
A class showing the contents of a memory module in a new window.
@@ -187,6 +198,7 @@ class IntegerMemoryTable(MemoryTable):
item = self.item(row, col)
value = item.text()
max_value = 2**self._bit_length
# Turn every value into a positive int
# in base 10
@@ -203,6 +215,10 @@ class IntegerMemoryTable(MemoryTable):
self._base == Base.DECIMAL_SIGNED or self._base == Base.DECIMAL_UNSIGNED
):
value = int(value)
if value > max_value:
raise ValueToBig
except ValueError:
msg = None
if self._base == Base.BINARY:
@@ -217,12 +233,18 @@ class IntegerMemoryTable(MemoryTable):
self.update()
return
except ValueToBig:
self._errorMessageWidget.showMessage(
"Input value does not fit within the bit length."
)
self.update()
return
# Turn negative signed numbers into their positive unsigned
# counter part
# Ex 4 bits:
# -7 = 1001
# -7 + 16 % 16 = 9 = 1001 = -7
max_value = 2**self._bit_length
value = (value + max_value) % max_value
index = row * 4 + col
Loading