What is the performance cost of throwing an exception in Dart?

Viewed 166

Coming from a .NET world where throwing an exception when the error is on the user side (like validation) is considered to be a bad practice, it appears really weird to see many instances of it in different articles and GitHub examples.

Is there any mechanism in Dart that makes stack tracing less expensive, or in Dart throwing an exception for something like input validation is bad practice as well?

1 Answers

As you said, exceptions should be used mainly for exceptional paths. Validation is actually a some part of program domain.

I did a very simple test - returning result vs exception. https://gist.github.com/jposert/0cbf824ac625a6563c2f62085eda64e8

test-results

The test isn't statistically correct but i think it shows that even though theres an difference in times - in context of the software it probably doesn't matter.

However, for sure it's incorrect from architectural point of view to throw instead of return.

Related