搜尋此網誌

2026年11月3日星期二

Errors and exceptions

Both errors and exceptions indicate that something has gone wrong in a program, but an exception is a specific type of error that Python can handle.


BaseException is the top-most class for all exceptions in Python.

BaseException
│
├── Exception
│   ├── ValueError
│   ├── TypeError
│   ├── IndexError
│   ├── KeyError
│   └── ZeroDivisionError
│
├── SystemExit
├── KeyboardInterrupt

└── GeneratorExit

A stack trace is a report that shows the sequence of function calls that led to an error.

It helps programmers answer:

"Where did the error happen, and how did the program get there?"


A nested call happens when one function calls another function, which may call another function, and so on.

def c():
    print("In c")
def b():
    c()
def a():
    b()

execution flow
a()
 ↓
b()
 ↓
c()
Here, a() calls b(), and b() calls c(). These are nested function calls.

intimidating: frightening in a way that makes a person feel less confident

try:
    1/0
except Exception as e:
    print(type(e))

ZeroDivisionError is a subclass of Exception
The variable e refers to the exception object.
e = exception object
type(e) = ZeroDivisionError

Microsoft Copilot

沒有留言:

發佈留言