How to make warning into error?

Julia doesn’t “throw” warnings. However, warning messages are typically written to the stderr stream, so you can turn such warnings to errors simply by redirecting stderr to a read-only stream:

julia> redirect_stderr(open(touch(tempname()), "r"))
IOStream(<file ...>)

julia> @warn "foo"
ERROR: ArgumentError: write failed, IOStream is not writeable

Originally posted by @stevengj in Terminate after warning is thrown - global setting -#2 by stevengj


Or, you can install a custom logger that turns warnings into errors:

julia> using Logging, LoggingExtras

julia> error_on_warning = EarlyFilteredLogger(global_logger()) do log_args
           if log_args.level >= Logging.Warn
               error(log_args)
           end
           return true
       end;

julia> global_logger(error_on_warning);

Adapted from @fredrikekre’s answer here

3 Likes