local $@; eval { # My code that can throw an error here might_throw_error(); }; if (my $exception = $@) { # Catch any error here. Error message in $@ warn "Caught random error: $exception\n"; } sub might_throw_error { # Flip a coin and throw an error if ( rand(1) > 0.5 ) { die "Random error!\n"; } }