set_error_handler

set_error_handler(?callable $callback, int $error_levels = E_ALL): ?callable

Registers a custom callback to take over error handling.

C C23

C23
#include <stdio.h>

typedef void (*ErrorHandler)(const char *msg);
static ErrorHandler error_handler = NULL;

static void set_error_handler(ErrorHandler handler) {
    error_handler = handler;
}

static void fail(const char *msg) {
    if (error_handler) error_handler(msg);
}

static void my_handler(const char *msg) {
    printf("caught: %s\n", msg);
    printf("continuing\n");
}

int main(void) {
    set_error_handler(my_handler);
    fail("boom");
    return 0;
}
Output
caught: boom
continuing

C++ C++26

C++26
#include <functional>
#include <iostream>
#include <string>

static std::function<void(const std::string &)> error_handler;

void set_error_handler(std::function<void(const std::string &)> handler) {
    error_handler = std::move(handler);
}

void fail(const std::string &msg) {
    if (error_handler) error_handler(msg);
}

int main() {
    set_error_handler([](const std::string &msg) {
        std::cout << "caught: " << msg << "\n";
        std::cout << "continuing\n";
    });
    fail("boom");
    return 0;
}
Output
caught: boom
continuing

C# v11.0.100

v11.0.100
AppDomain.CurrentDomain.UnhandledException += (_, e) =>
{
    Console.WriteLine("caught: " + ((Exception)e.ExceptionObject).Message);
    Console.WriteLine("continuing");
    Environment.Exit(0);
};
throw new Exception("boom");
Output
caught: boom
continuing

Go v1.27.4

v1.27.4
package main

import "fmt"

func main() {
    defer func() {
        if r := recover(); r != nil {
            fmt.Println("caught:", r)
            fmt.Println("continuing")
        }
    }()
    panic("boom")
}
Output
caught: boom
continuing

Java v27.0.1

v27.0.1
public class Main {
    public static void main(String[] args) {
        Thread.setDefaultUncaughtExceptionHandler((thread, e) -> {
            System.out.println("caught: " + e.getMessage());
            System.out.println("continuing");
            System.exit(0);
        });
        throw new RuntimeException("boom");
    }
}
Output
caught: boom
continuing

JavaScript v26.13.1

v26.13.1
process.on("uncaughtException", (e) => {
  console.log("caught: " + e.message);
  console.log("continuing");
});
throw new Error("boom");
Output
caught: boom
continuing

PHP v8.6.0

v8.6.0
set_error_handler(function ($no, $str) {
    echo "caught: $str\n";
    return true;
});
trigger_error("boom", E_USER_WARNING);
echo "continuing";
Output
caught: boom
continuing

Python v3.15.2

v3.15.2
import warnings

def custom_showwarning(message, category, filename, lineno, file=None, line=None):
    print("caught:", message)

warnings.showwarning = custom_showwarning
warnings.warn("boom", UserWarning)
print("continuing")
Output
caught: boom
continuing

Rust v1.99.1

v1.99.1
use std::panic;

fn main() {
    panic::set_hook(Box::new(|info| {
        let msg = info
            .payload()
            .downcast_ref::<&str>()
            .copied()
            .unwrap_or("unknown");
        println!("caught: {}", msg);
    }));
    let _ = panic::catch_unwind(|| panic!("boom"));
    println!("continuing");
}
Output
caught: boom
continuing

TypeScript v26.13.1

v26.13.1
process.on("uncaughtException", (e: Error) => {
  console.log("caught: " + e.message);
  console.log("continuing");
});
throw new Error("boom");
Output
caught: boom
continuing

Language differences

C: no global error hook — the idiom is registering callbacks via function pointers (as with X11's XSetErrorHandler).

C++: no global error hook — callbacks via std::function.

C#: uses AppDomain.UnhandledException; finish with Environment.Exit(0) because the process would otherwise still exit non-zero.

Go: uses defer + recover.

Java: uses Thread.setDefaultUncaughtExceptionHandler; finish with System.exit(0) because the process would otherwise still exit non-zero.

JavaScript: uses process.on("uncaughtException") (window.onerror in browsers).

PHP: set_error_handler registers a callback that takes over warning/notice-level errors; returning true from the callback suppresses the built-in handling and lets the script continue (fatal errors cannot be intercepted).

Python: replaces the warnings.showwarning hook (uncaught exceptions have a separate sys.excepthook).

Rust: combines panic::set_hook for custom output with catch_unwind to actually continue.

TypeScript: same as JS — process.on("uncaughtException").