Key Concepts Notes of Computer Science
Key Concepts Notes of Computer Science
1. Exception: An error or unexpected event that occurs during the execution of a program, disrupting its normal
flow.
2. Exception Handling Mechanism: A way to gracefully manage exceptions, ensuring the program can continue
running or terminate cleanly.
3. Try Block: A block of code where exceptions are anticipated. If an exception occurs within this block, control is
transferred to the associated catch block.
4. Catch Block: A block that contains code to handle the exception. Each catch block is designed to handle a
specific type of exception.
5. Finally Block: A block that always executes, regardless of whether an exception was thrown or not. It is typically
used for cleanup activities.
6. Throwing an Exception: The process of signaling the occurrence of an exception, either by the system or
manually within the code using the throw statement.
7. Built-in Exceptions: Standard exceptions provided by the programming language, such as NullPointerException
in Java or IndexError in Python.
8. Custom Exceptions: User-defined exceptions tailored to specific needs, created by extending the existing
exception classes.
def pop(self):
if not self.is_empty():
return self.items.pop()
return None
def peek(self):
if not self.is_empty():
return self.items[-1]
return None
def is_empty(self):
return len(self.items) == 0
def size(self):
return len(self.items)
# Example usage
stack = Stack()
stack.push(1)
stack.push(2)
stack.push(3)
print(stack.pop()) # Output: 3
print(stack.peek()) # Output: 2
print(stack.size()) # Output: 2