error_clear_last
error_clear_last(): void
Clears the record of the most recent error.
C C23
#include <errno.h>
#include <stdio.h>
int main(void) {
fopen("/no/such/file", "r"); // fails and sets errno
printf("has error: %d\n", errno != 0);
errno = 0; // C has no error_clear_last; resetting errno is the manual equivalent
printf("cleared: %d\n", errno == 0);
return 0;
}
has error: 1 cleared: 1
C++ C++26
#include <cerrno>
#include <cstdio>
int main() {
std::fopen("/no/such/file", "r"); // fails and sets errno
std::printf("has error: %d\n", errno != 0);
errno = 0; // manual reset, same as C
std::printf("cleared: %d\n", errno == 0);
return 0;
}
has error: 1 cleared: 1
C# v11.0.100
Exception? last = null;
try
{
throw new InvalidOperationException("boom");
}
catch (InvalidOperationException e)
{
last = e;
}
last = null; // no global error record exists; drop your own reference to "clear"
Console.WriteLine(last == null);
True
Go v1.27.4
package main
import (
"errors"
"fmt"
)
func main() {
err := errors.New("boom")
fmt.Println(err != nil)
err = nil
fmt.Println(err == nil)
}
true true
Java v27.0.1
public class Main {
public static void main(String[] args) {
Throwable last = null;
try {
throw new RuntimeException("boom");
} catch (RuntimeException e) {
last = e;
}
last = null;
System.out.println(last == null);
}
}
true
JavaScript v26.13.1
let last = null;
try {
throw new Error("boom");
} catch (e) {
last = e;
}
last = null;
console.log(last === null);
true
PHP v8.6.0
@$undefined_var;
var_dump(error_get_last() !== null);
error_clear_last();
var_dump(error_get_last() === null);
bool(true) bool(true)
Python v3.15.2
import sys
try:
raise ValueError("boom")
except ValueError:
pass
print(sys.exc_info() == (None, None, None))
True
Rust v1.99.1
fn main() {
let last: Result<(), String> = Err("boom".to_string());
println!("{}", last.is_err());
let last: Result<(), String> = Ok(()); // rebind (or drop) to clear; no global state exists
println!("{}", last.is_ok());
}
true true
TypeScript v26.13.1
let last: Error | null = null;
try {
throw new Error("boom");
} catch (e) {
last = e as Error; // catch variable defaults to unknown
}
last = null;
console.log(last === null);
true
Language differences
C: errno must be reset to 0 manually (the idiom is clearing before the call).
C++: same as C — errno must be reset manually.
C#: errors live in local exception objects and disappear when the scope ends or the binding changes — no global record to clear.
Go: err is an ordinary value, so assigning nil suffices.
Java: errors live in local exception objects and disappear when the scope ends — no global record to clear.
JavaScript: errors live in local exception objects and disappear when the scope ends — no global record to clear.
PHP: PHP-specific — the error record is process-wide global state, and error_clear_last() wipes it before checking whether "a new error happened after this point" (often paired with error_get_last as a try/catch-style probe).
Python: exception state is cleared automatically when leaving the except block (sys.exc_info() back to (None, None, None)).
Rust: errors live in Result values and disappear when the binding changes — no global record to clear.
TypeScript: same as JavaScript — no global record to clear.