# Error

```python
raise gradio.Error("An error occurred 💥!", duration=5)
```

### Description

This class allows you to pass custom error messages to the user. You can do so by raising a gr.Error("custom message") anywhere in the code, and when that line is executed the custom message will appear in a modal on the demo.

You can control for how long the error message is displayed with the `duration` parameter. If it’s `None`, the message will be displayed forever until the user closes it. If it’s a number, it will be shown for that many seconds.

You can also hide the error modal from being shown in the UI by setting `visible=False`.

Below is a demo of how different values of duration control the error, info, and warning messages. You can see the code here.

### Example Usage

```python
import gradio as gr
def divide(numerator, denominator):
    if denominator == 0:
        raise gr.Error("Cannot divide by zero!")
gr.Interface(divide, ["number", "number"], "number").launch()
```

### Initialization

| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `message` | `str` | `"Error raised."` | The error message to be displayed to the user. Can be HTML, which will be rendered in the modal. |
| `duration` | `float \| None` | `10` | The duration in seconds to display the error message. If None or 0, the error message will be displayed until the user closes it. |
| `visible` | `bool` | `True` | Whether the error message should be displayed in the UI. |
| `title` | `str` | `"Error"` | The title to be displayed to the user at the top of the error modal. |
| `print_exception` | `bool` | `True` | Whether to print traceback of the error to the console when the error is raised. |
### Demos

**calculator**

[See demo on Hugging Face Spaces](https://huggingface.co/spaces/gradio/calculator)

```python
import gradio as gr

def calculator(num1, operation, num2):
    if operation == "add":
        return num1 + num2
    elif operation == "subtract":
        return num1 - num2
    elif operation == "multiply":
        return num1 * num2
    elif operation == "divide":
        if num2 == 0:
            raise gr.Error("Cannot divide by zero!")
        return num1 / num2

demo = gr.Interface(
    calculator,
    [
        "number",
        gr.Radio(["add", "subtract", "multiply", "divide"]),
        "number"
    ],
    "number",
    examples=[
        [45, "add", 3],
        [3.14, "divide", 2],
        [144, "multiply", 2.5],
        [0, "subtract", 1.2],
    ],
    title="Toy Calculator",
    description="Here's a sample toy calculator.",
    api_name="predict"
)

if __name__ == "__main__":
    demo.launch()
```

**blocks_chained_events**

[See demo on Hugging Face Spaces](https://huggingface.co/spaces/gradio/blocks_chained_events)

```python
import gradio as gr

def failure():
    raise gr.Error("This should fail!")

def exception():
    raise ValueError("Something went wrong")

def success():
    return True

def warning_fn():
    gr.Warning("This is a warning!")

def info_fn():
    gr.Info("This is some info")

with gr.Blocks() as demo:
    gr.Markdown("Used in E2E tests of success event trigger. The then event covered in chatbot E2E tests."
                " Also testing that the status modals show up.")
    with gr.Row():
        result = gr.Textbox(label="Result")
        result_2 = gr.Textbox(label="Consecutive Event")
        result_failure = gr.Textbox(label="Failure Event")
    with gr.Row():
        success_btn = gr.Button(value="Trigger Success")
        success_btn_2 = gr.Button(value="Trigger Consecutive Success")
        failure_btn = gr.Button(value="Trigger Failure")
        failure_exception = gr.Button(value="Trigger Failure With ValueError")
    with gr.Row():
        trigger_warning = gr.Button(value="Trigger Warning")
        trigger_info = gr.Button(value="Trigger Info")

        success_btn_2.click(success, None, None).success(lambda: "First Event Trigered", None, result).success(lambda: "Consecutive Event Triggered", None, result_2)
        success_event = success_btn.click(success, None, None)
        success_event.success(lambda: "Success event triggered", inputs=None, outputs=result)
        success_event.failure(lambda: "Should not be triggered", inputs=None, outputs=result_failure)
        failure_event = failure_btn.click(failure, None, None)
        failure_event.success(lambda: "Should not be triggered", inputs=None, outputs=result)
        failure_event.failure(lambda: "Failure event triggered", inputs=None, outputs=result_failure)
        failure_exception.click(exception, None, None)
        trigger_warning.click(warning_fn, None, None)
        trigger_info.click(info_fn, None, None)

if __name__ == "__main__":
    demo.launch(show_error=True)
```

- [Alerts](https://www.gradio.app/guides/alerts/)
