throw(ErrorException(...)) vs. @error

For development / debugging, I’ve been using throw(ErrorException(...)), and @assert. As this code moves to production, where it will be in a batch job running in “the cloud”, I’d like to have such errors logged.

What’s the recommended Julia way to do this? Should I develop using @error, rather than @assert, and so forth?

2 Likes

@error is for logging, throw is for throwing an exception. They are quite different things so depending on what you want to happen you would chose differently what to use.

1 Like

I understand that they are different. My point was that I want the same code (detecting some problem) to do different things depending on the context. Put me in a debugger with a stack trace if I’m running interactively. Log if the same code is running in “batch mode” on a server.

Is there a smooth Julia way to do that?

I have a couple of apps in production and the following has served me well.

I use @error to log error messages with context (i.e.variables that might help me troubleshoot the problem), immediately followed by error if the exception needs to be propagated to the caller.

I use the same same code regardless of the deployment environment.

I don’t use @assert in production because I generally want to recover from the error than crashing the program. That might be helpful in tests? I would be delighted to hear about other’s opinions.

1 Like