Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

13
  • 17
    "exception throwing is destroying performance" - That depends on the context in which they're used, and also varies strongly from one language to another. I don't think such sweeping generalizations are very helpful. Commented Jun 6, 2024 at 13:47
  • 3
    @marcelm Definitely true. While exceptions are known to be very expensive (compared to other control flows) in the C++/Java/C# class of languages, Python's exception handling is sufficiently fast to be used in the generator syntax (StopIteration), and that is so fast that it shows up in the basic underlying flow of asynchronous coroutines. Commented Jun 6, 2024 at 14:45
  • 9
    Throwing exceptions is expensive This is an urban legend as far as the JVM is concerned. Creating exceptions with stack traces is expensive, but the unwinding operation is not. Commented Jun 6, 2024 at 18:13
  • 3
    @chrylis-cautiouslyoptimistic- IIRC, exception handling was a lot more expensive in the very early days of Java (so the urban legend probably has a grain of truth) — but then it got optimised.  And since exceptions aren't thrown very often (you're not using them for normal control flow, I hope!), it's not usually worth worrying about them; it's almost always more important to think about what makes sense for your program (what makes the code easier to understand and easier to maintain), and not think about low-level performance unless it becomes a problem. Commented Jun 6, 2024 at 20:13
  • 1
    @Ccm Even in C++ of all languages, the current C++ Core Guidelines (FWIW) recommend the use of exceptions, even saying "We don't consider 'performance' a valid reason not to use exceptions." (source: github.com/isocpp/CppCoreGuidelines/blob/master/…) Commented Jun 6, 2024 at 23:38