trigger_error
trigger_error(string $message, int $error_level = E_USER_NOTICE): bool
Raises a user-level error or warning from user code.
C C23
#include <errno.h>
#include <stdio.h>
static int risky(void) {
errno = EINVAL;
return -1;
}
int main(void) {
if (risky() != 0) {
printf("handled: invalid argument (errno %d)\n", errno);
}
printf("continuing\n");
return 0;
}
handled: invalid argument (errno 22) continuing
C++ C++26
#include <iostream>
#include <stdexcept>
void risky() {
throw std::runtime_error("something broke");
}
int main() {
try {
risky();
} catch (const std::runtime_error &e) {
std::cout << "handled: " << e.what() << "\n";
}
std::cout << "continuing\n";
return 0;
}
handled: something broke continuing
C# v11.0.100
static void Risky()
{
throw new InvalidOperationException("something broke");
}
try
{
Risky();
}
catch (InvalidOperationException e)
{
Console.WriteLine("handled: " + e.Message);
}
Console.WriteLine("continuing");
handled: something broke continuing
Go v1.27.4
package main
import (
"errors"
"fmt"
)
func risky() error {
return errors.New("something broke")
}
func main() {
if err := risky(); err != nil {
fmt.Println("handled:", err)
}
fmt.Println("continuing")
}
handled: something broke continuing
Java v27.0.1
public class Main {
static void risky() {
throw new IllegalStateException("something broke");
}
public static void main(String[] args) {
try {
risky();
} catch (IllegalStateException e) {
System.out.println("handled: " + e.getMessage());
}
System.out.println("continuing");
}
}
handled: something broke continuing
JavaScript v26.13.1
console.log("Warning: something broke");
console.log("continuing");
Warning: something broke continuing
PHP v8.6.0
set_error_handler(function ($no, $str) {
echo "handled: $str (level $no)\n";
return true;
});
trigger_error("something broke", E_USER_WARNING);
echo "continuing";
handled: something broke (level 512) continuing
Python v3.15.2
import warnings
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
warnings.warn("something broke", UserWarning)
print(caught[0].message)
print("continuing")
something broke continuing
Rust v1.99.1
fn risky() -> Result<(), String> {
Err(String::from("something broke"))
}
fn main() {
match risky() {
Ok(_) => {}
Err(e) => println!("handled: {}", e),
}
println!("continuing");
}
handled: something broke continuing
TypeScript v26.13.1
console.log("Warning: something broke");
console.log("continuing");
Warning: something broke continuing
Language differences
C: sets errno and returns an error code (EINVAL=22).
C++: a plain throw.
C#: a plain throw.
Go: conventionally returns an errors.New(...) error value.
Java: a plain throw.
JavaScript: uses console.warn (the browser/Node convention, which writes to stderr — the example uses console.log with a prefix to keep stdout ordering).
PHP: trigger_error raises a user-level error (E_USER_WARNING has constant value 512); combined with set_error_handler it can be caught and execution continues, otherwise it goes to the standard error output.
Python: uses warnings.warn for soft warnings (the example collects them via catch_warnings(record=True)).
Rust: returns an Err(...) value.
TypeScript: same as JS — console.warn.