Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Sutter/Alexandrescu explanation of Exceptions

Sutter/Alexandrescu explanation of Exceptions

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 2601640
    wrote on last edited by
    #1

    Sutter and Alexandrescu have a section on proper use of C++ exceptions that strongly encourages the use of C++ exceptions. I am confused by the last item, number 75, where they suggest avoiding exception specifications. 1) If you do not throw, then why bother trying to catch, which is what the previous items advocate? 2) If exception specifications are bad why does C++ support them? Can someone please explain what I am missing? Thanks

    S N 2 Replies Last reply
    0
    • U User 2601640

      Sutter and Alexandrescu have a section on proper use of C++ exceptions that strongly encourages the use of C++ exceptions. I am confused by the last item, number 75, where they suggest avoiding exception specifications. 1) If you do not throw, then why bother trying to catch, which is what the previous items advocate? 2) If exception specifications are bad why does C++ support them? Can someone please explain what I am missing? Thanks

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      Member 2603772 wrote:

      If you do not throw, then why bother trying to catch, which is what the previous items advocate?

      Not using exception specifications doesn't mean you are guaranteeing that your code doesn't throw an exception - it's the reverse of that - you are making no guarantees about whether your code throws exceptions or not. If a piece of code you're calling has no exception specification, assume that it may throw *any* exception. Only handle exceptions if there's some action you can perform that fixes what caused the exception. Any other exception should be allowed to terminate the application - if you don't know how to fix what caused the exception, then you've got problems.

      Member 2603772 wrote:

      If exception specifications are bad why does C++ support them?

      They're not bad per se. The point is that C++ doesn't enforce them or (really) take any notice of them. That means that compilers won't verify that they're correct, so they're liable (like code comments) to become inaccurate as you change the code they describe, because you probably won't remember to change the exception specification. This article[^] by Herb Sutter may help to make his view on exception specifications clearer?

      1 Reply Last reply
      0
      • U User 2601640

        Sutter and Alexandrescu have a section on proper use of C++ exceptions that strongly encourages the use of C++ exceptions. I am confused by the last item, number 75, where they suggest avoiding exception specifications. 1) If you do not throw, then why bother trying to catch, which is what the previous items advocate? 2) If exception specifications are bad why does C++ support them? Can someone please explain what I am missing? Thanks

        N Offline
        N Offline
        Nemanja Trifunovic
        wrote on last edited by
        #3

        Maybe this article[^] helps you understand the exception specifications and why they are not a good idea.

        Programming Blog utf8-cpp

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups