error_get_last

error_get_last(): ?array

Returns the most recent error that occurred (type/message/file/line).

C C23

C23
#include <errno.h>
#include <stdio.h>
#include <string.h>
int main(void) {
    errno = 0; // errno is the closest thing to a "last error" global in C
    FILE *f = fopen("/no/such/file", "r");
    if (f == NULL) {
        printf("%d: %s", errno, strerror(errno));
    } else {
        fclose(f);
    }
    return 0;
}
Output
2: No such file or directory

C++ C++26

C++26
#include <cerrno>
#include <cstdio>
#include <cstring>
int main() {
    errno = 0; // errno is the closest thing to a "last error" global in C/C++
    FILE *f = std::fopen("/no/such/file", "r");
    if (f == nullptr) {
        std::printf("%d: %s", errno, std::strerror(errno));
    } else {
        std::fclose(f);
    }
    return 0;
}
Output
2: No such file or directory

C# v11.0.100

v11.0.100
try
{
    int zero = 0;
    int d = 1 / zero; // runtime division by zero (a literal 1 / 0 fails at compile time)
    Console.WriteLine(d);
}
catch (DivideByZeroException e) // the caught object is the "last error"; there is no global state
{
    Console.WriteLine(e.GetType().Name + ": " + e.Message);
}
Output
DivideByZeroException: Attempted to divide by zero.

Go v1.27.4

v1.27.4
package main

import (
    "fmt"
    "os"
)

func main() {
    _, err := os.Open("/no/such/file")
    fmt.Println("Open: " + err.Error())
}
Output
Open: open /no/such/file: no such file or directory

Java v27.0.1

v27.0.1
public class Main {
    public static void main(String[] args) {
        try {
            int d = 1 / 0;
            System.out.println(d);
        } catch (ArithmeticException e) {
            System.out.println(e.getClass().getSimpleName() + ": " + e.getMessage());
        }
    }
}
Output
ArithmeticException: / by zero

JavaScript v26.13.1

v26.13.1
try {
  decodeURIComponent("%");
} catch (e) {
  console.log(e.name + ": " + e.message);
}
Output
URIError: URI malformed

PHP v8.6.0

v8.6.0
@$undefined_var;
$e = error_get_last();
echo $e["type"], ":", $e["message"];
Output
2:Undefined variable $undefined_var

Python v3.15.2

v3.15.2
try:
    n = 1 / 0
    print(n)
except ZeroDivisionError as e:
    print(type(e).__name__ + ": " + str(e))
Output
ZeroDivisionError: division by zero

Rust v1.99.1

v1.99.1
fn main() {
    // std::fs returns io::Result; the Err value carries the last error (no global state)
    let r = std::fs::File::open("/no/such/file");
    if let Err(e) = r {
        println!("{}", e);
    }
}
Output
No such file or directory (os error 2)

TypeScript v26.13.1

v26.13.1
try {
  decodeURIComponent("%");
} catch (e) {
  console.log((e as Error).name + ": " + (e as Error).message);
}
Output
URIError: URI malformed

Language differences

C: the closest semantic is the errno global plus strerror (2 = ENOENT, no such file).

C++: same as C — errno + strerror.

C#: errors surface as exception objects — the caught instance is "this" error.

Go: errors are values passed explicitly (the err variable is the last error).

Java: errors surface as exception objects — the caught instance is "this" error.

JavaScript: errors surface as exception objects — the caught instance is "this" error.

PHP: keeps a process-wide "last error" record — error_get_last() returns an associative array (type severity, message, file, line); errors suppressed by @ are recorded too, and combined with register_shutdown_function it can capture fatal errors.

Python: errors surface as exception objects (the caught instance is "this" error; sys.exc_info() is only valid inside the except block and is cleared afterwards).

Rust: carries errors in a Result's Err variant.

TypeScript: same as JavaScript.