Modify the exception handling in Java and Python.

This commit is contained in:
krahets
2023-04-23 03:41:39 +08:00
parent 7e59e2c7fb
commit 3590262c7e
14 changed files with 35 additions and 28 deletions

View File

@ -26,12 +26,14 @@ class ArrayStack:
def pop(self) -> int:
"""出栈"""
assert not self.is_empty(), "栈为空"
if self.is_empty():
raise IndexError("栈为空")
return self.__stack.pop()
def peek(self) -> int:
"""访问栈顶元素"""
assert not self.is_empty(), "栈为空"
if self.is_empty():
raise IndexError("栈为空")
return self.__stack[-1]
def to_list(self) -> list[int]: