restore_error_handler
restore_error_handler(): true
Removes the current error handler and restores the previous one.
C C23
#include <stdio.h>
typedef void (*Handler)(const char *msg);
static Handler stack[8]; // handler stack (restore = pop)
static int top = 0;
static void push_handler(Handler h) { stack[top++] = h; }
static void pop_handler(void) { top--; } // restore = pop back to the previous handler
static void fail(const char *msg) { if (top > 0) stack[top - 1](msg); }
static void first(const char *msg) { printf("first: %s\n", msg); }
static void second(const char *msg) { printf("second: %s\n", msg); }
int main(void) {
push_handler(first);
push_handler(second);
fail("boom");
pop_handler();
fail("boom");
return 0;
}
second: boom first: boom
C++ C++26
#include <functional>
#include <iostream>
#include <string>
#include <vector>
static std::vector<std::function<void(const std::string &)>> stack; // handler stack
void push_handler(std::function<void(const std::string &)> h) { stack.push_back(std::move(h)); }
void pop_handler() { stack.pop_back(); } // restore = pop back to the previous handler
void fail(const std::string &msg) { if (!stack.empty()) stack.back()(msg); }
int main() {
push_handler([](const std::string &m) { std::cout << "first: " << m << "\n"; });
push_handler([](const std::string &m) { std::cout << "second: " << m << "\n"; });
fail("boom");
pop_handler();
fail("boom");
return 0;
}
second: boom first: boom
C# v11.0.100
var stack = new Stack<Action<string>>(); // handler stack
stack.Push(m => Console.WriteLine($"first: {m}"));
stack.Push(m => Console.WriteLine($"second: {m}"));
stack.Peek()("boom");
stack.Pop(); // restore = pop back to the previous handler
stack.Peek()("boom");
second: boom first: boom
Go v1.27.4
package main
import "fmt"
type Handler func(msg string)
var stack []Handler
func push(h Handler) { stack = append(stack, h) }
func pop() { stack = stack[:len(stack)-1] }
func fail(msg string) { stack[len(stack)-1](msg) }
func main() {
push(func(msg string) { fmt.Println("first:", msg) })
push(func(msg string) { fmt.Println("second:", msg) })
fail("boom")
pop()
fail("boom")
}
second: boom first: boom
Java v27.0.1
import java.util.ArrayDeque;
import java.util.Deque;
public class Main {
interface Handler {
void handle(String msg);
}
static final Deque<Handler> stack = new ArrayDeque<>();
static void fail(String msg) {
stack.peek().handle(msg);
}
public static void main(String[] args) {
stack.push(m -> System.out.println("first: " + m));
stack.push(m -> System.out.println("second: " + m));
fail("boom");
stack.pop();
fail("boom");
}
}
second: boom first: boom
JavaScript v26.13.1
const stack = [];
const fail = (msg) => stack[stack.length - 1](msg);
stack.push((msg) => console.log("first: " + msg));
stack.push((msg) => console.log("second: " + msg));
fail("boom");
stack.pop();
fail("boom");
second: boom first: boom
PHP v8.6.0
set_error_handler(function ($no, $str) { echo "first: $str\n"; });
set_error_handler(function ($no, $str) { echo "second: $str\n"; });
trigger_error("boom", E_USER_WARNING);
restore_error_handler();
trigger_error("boom", E_USER_WARNING);
second: boom first: boom
Python v3.15.2
import warnings
stack = []
def push(handler):
stack.append(handler)
warnings.showwarning = handler
def pop():
stack.pop()
warnings.showwarning = stack[-1]
def first(message, *args):
print("first:", message)
def second(message, *args):
print("second:", message)
push(first)
push(second)
warnings.warn("boom", UserWarning)
pop()
warnings.warn("boom", UserWarning)
second: boom first: boom
Rust v1.99.1
use std::panic;
fn main() {
panic::set_hook(Box::new(|info| {
println!("first: {}", info.payload().downcast_ref::<&str>().copied().unwrap_or("?"));
}));
let prev = panic::take_hook(); // take_hook removes and returns the current hook
panic::set_hook(Box::new(|info| {
println!("second: {}", info.payload().downcast_ref::<&str>().copied().unwrap_or("?"));
}));
let _ = panic::catch_unwind(|| panic!("boom"));
panic::set_hook(prev); // reinstall the previous handler
let _ = panic::catch_unwind(|| panic!("boom"));
}
second: boom first: boom
TypeScript v26.13.1
const stack: Array<(msg: string) => void> = []; // explicit handler type
const fail = (msg: string): void => stack[stack.length - 1](msg);
stack.push((msg) => console.log("first: " + msg));
stack.push((msg) => console.log("second: " + msg));
fail("boom");
stack.pop(); // restore = pop back to the previous handler
fail("boom");
second: boom first: boom
Language differences
C: no ready-made stack API — simulated with a custom stack (function pointers).
C++: simulated with a custom stack (std::function).
C#: no ready-made stack API — simulated with a custom stack.
Go: simulated with a custom stack; the native recovery mechanism is defer + recover, but once a panic is recovered it is finished — there is no layer-by-layer restore semantics.
JavaScript: no ready-made stack API — simulated with a custom stack.
PHP: error handlers are stack-shaped by nature — every set_error_handler pushes, and restore_error_handler pops back to the previous handler (once empty, the built-in handling resumes).
Python: other languages have no ready-made stack API — replace warnings.showwarning and save/restore the old value in a list by hand.
Rust: the only one with a built-in "take the previous" — panic::take_hook returns the current hook so set_hook(prev) restores it.
TypeScript: same as JS — a custom stack.