throw vs error

307 views
Skip to first unread message

Michele Zaffalon

unread,
Aug 21, 2015, 11:32:16 AM8/21/15
to julia-users
In what cases should one use error() instead of throw()? The manual is not particular clear about the difference:

The error() function is used to produce an ErrorException that interrupts the normal flow of control.

Isn't what throw is supposed to do?

Isaiah Norton

unread,
Aug 21, 2015, 11:43:15 AM8/21/15
to julia...@googlegroups.com
`error` is generic, whereas `throw` can raise typed errors, such as DomainError, SimdError, UVError, etc. which may have special handling -- for example, customized `show` methods to print help/suggestions to resolve the specific situation.

Michele Zaffalon

unread,
Aug 21, 2015, 11:51:47 AM8/21/15
to julia...@googlegroups.com
That is my point: error is the same as throw(ErrorException). Should both co-exist? Is error just a short name for the throw(ErrorException) version?

Isaiah Norton

unread,
Aug 21, 2015, 11:53:53 AM8/21/15
to julia...@googlegroups.com
Yes.

Stefan Karpinski

unread,
Aug 21, 2015, 12:07:25 PM8/21/15
to Julia Users
This is actually an old debate between me and Jeff. The distinction I tried to make was that `throw` should be used with catch as a form of control flow, while `error` should be used when there's an actual error. However, that distinction hasn't stuck, possibly because he never liked it and it's common to see `throw(InexactError())` in Base. We could get rid of `error` but writing `throw(ErrorException("oops"))` to throw a simple error seems pretty unpalatable.

Michele Zaffalon

unread,
Aug 22, 2015, 12:58:42 AM8/22/15
to julia...@googlegroups.com
Verbosity aside, (which may also be disputable since errors are supposed to be rare...), I still do not see the difference even from reading the constructed examples in the manual: both are examples of functions that are not defined for negative arguments, but in the first case, the function throws an exception, in the second it signals an error.
michele

Stefan Karpinski

unread,
Aug 22, 2015, 4:15:42 AM8/22/15
to Julia Users
Not sure how that's really a response to what I said, which acknowledges that it's an ambiguity...

Michele Zaffalon

unread,
Aug 22, 2015, 7:06:43 AM8/22/15
to julia...@googlegroups.com
You are right, my reply was unnecessary.

Stefan Karpinski

unread,
Aug 22, 2015, 11:07:19 AM8/22/15
to Julia Users
No worries, if you've got any bright ideas how we should resolve this throw/error thing...

Michele Zaffalon

unread,
Aug 23, 2015, 12:26:10 AM8/23/15
to julia...@googlegroups.com
Bright ideas? I am afraid not.

Had you said dumb ideas, I would have suggested either to remove it completely, leave it as a shortname for throw(ErrorException) or to replace with something that cannot be caught, but for this one I cannot suggest a use case (maybe writing way past array boundaries when using @inbounds?)

Jeffrey Sarnoff

unread,
Aug 23, 2015, 4:31:55 AM8/23/15
to julia-users

Calling error("message") should report an error from the function in which it is called, and it would be helpful to show the call chain. Currently, calling error reports an error from the top of the call chain, the function generating the error is not shown. Throwing an exception is either caught and handled, caught and rethrown, or uncaught. Uncaught, it should travel up the call chain -- looking to be caught, and remembering the calls it traverses in reverse; at the top level, report it as an uncaught exception with the call chain.

If it is thrown, it presumed handleable and so it is an exception.  If it called and not thrown, it is presumed unhandleable and so it is an error.

Michele Zaffalon

unread,
Aug 23, 2015, 10:38:10 PM8/23/15
to julia...@googlegroups.com
to replace with something that cannot be caught, but for this one I cannot suggest a use case (maybe writing way past array boundaries when using @inbounds?)

Maybe a better example for something that should not be possible to catch is an assertion error.

ele...@gmail.com

unread,
Aug 24, 2015, 12:41:51 AM8/24/15
to julia-users


On Monday, August 24, 2015 at 12:38:10 PM UTC+10, Michele Zaffalon wrote:
 
to replace with something that cannot be caught, but for this one I cannot suggest a use case (maybe writing way past array boundaries when using @inbounds?)

Maybe a better example for something that should not be possible to catch is an assertion error.

But then catching assertions is useful for test code that deliberately triggers them.

Michele Zaffalon

unread,
Aug 24, 2015, 1:14:57 AM8/24/15
to julia...@googlegroups.com
to replace with something that cannot be caught, but for this one I cannot suggest a use case (maybe writing way past array boundaries when using @inbounds?)

Maybe a better example for something that should not be possible to catch is an assertion error.

But then catching assertions is useful for test code that deliberately triggers them.

I thought the meaning of assertion is "I strongly believe that at this point in the program this statement is true; if not, something is really wrong and I had better should stop immediately before other damage occurs". This could happen because not all possible error situations were considered.

For code testing, exceptions should be triggered instead: trying to open a non existing file? throw(FileNotFoundError)? square root of negative number? DomainError and so on.

Reply all
Reply to author
Forward
0 new messages