> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Python Raise Statement

The `raise` statement is a control flow mechanism used to explicitly trigger an exception during program execution. It interrupts normal execution flow and transfers control to the nearest enclosing exception handler (`except` block) that matches the raised exception type. If no appropriate handler is found, the interpreter terminates the program and prints a traceback.

## Syntax

```python theme={"dark"}
raise [exception_expression [from cause_expression]]
```

The `exception_expression` must evaluate to either an instance of a class deriving from `BaseException`, or a class deriving from `BaseException`.

## Execution Mechanics

The `raise` statement operates in four distinct forms based on the provided arguments:

### 1. Raising an Exception Instance

When provided with an instantiated exception object, `raise` throws that specific instance. This allows for the inclusion of custom error messages or attributes.

```python theme={"dark"}
raise ValueError("Invalid parameter type")
```

### 2. Raising an Exception Class

If `exception_expression` is an exception class rather than an instance, the Python interpreter implicitly instantiates the class by calling its constructor with no arguments.

```python theme={"dark"}
raise TypeError

# This is functionally identical to: raise TypeError()
```

### 3. Re-raising an Active Exception (Bare `raise`)

When `raise` is executed without any arguments, it re-raises the active exception currently being handled anywhere in the call stack. This can occur directly inside an `except` block or within a function invoked during exception handling. This preserves the original exception object, including its exact traceback and type, allowing an intermediate handler to inspect the exception before passing it up the call stack.

```python theme={"dark"}
try:
    1 / 0
except ZeroDivisionError:
    # Performs operations, then propagates the exact same exception
    raise
```

*Note: If a bare `raise` is executed where no exception is currently active, Python raises a `RuntimeError`.*

### 4. Exception Chaining (`raise ... from ...`)

The `from` clause enables explicit exception chaining. The `cause_expression` must evaluate to an exception instance, an exception class (which the interpreter will implicitly instantiate), or `None`.

When an exception is raised using `from`, the raised exception's `__cause__` attribute is set to the evaluated `cause_expression`. The interpreter uses this to format a traceback showing both exceptions, indicating that one directly caused the other.

```python theme={"dark"}
try:
    my_dict = {}
    my_dict["missing"]
except KeyError as e:
    raise RuntimeError("Processing failed") from e
```

If `from None` is used, it explicitly disables automatic exception chaining. By default, raising a new exception while another exception is being handled sets the new exception's `__context__` attribute to the active exception. `from None` suppresses this behavior, preventing the original exception from appearing in the traceback.

```python theme={"dark"}
try:
    my_dict = {}
    my_dict["missing"]
except KeyError:
    raise RuntimeError("Processing failed") from None
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Python Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
