Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. What's the best way to stop at an error in a program?

What's the best way to stop at an error in a program?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 586 Views 2 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.
  • K Offline
    K Offline
    kitfox
    wrote on last edited by
    #1

    I come from a Java background and am used to throwing an exception when I check something and find it has an invalid value. I tend to use them much like assert statements - I'm just checking to make sure the program has not entered an invalid state, and if it has I want the program to halt at that point and print an error message. I rarely catch thrown exceptions since usually they represent faulty behavior and there is little point in continuing.

    What would be the best way to do something similar in Qt/C++? I've tried adding in throw clauses, but they cause my program to exit without printing an error message:

    if (index < 0)
        throw "Index should be greater than 0";
    
    raven-worxR E 2 Replies Last reply
    0
    • K kitfox

      I come from a Java background and am used to throwing an exception when I check something and find it has an invalid value. I tend to use them much like assert statements - I'm just checking to make sure the program has not entered an invalid state, and if it has I want the program to halt at that point and print an error message. I rarely catch thrown exceptions since usually they represent faulty behavior and there is little point in continuing.

      What would be the best way to do something similar in Qt/C++? I've tried adding in throw clauses, but they cause my program to exit without printing an error message:

      if (index < 0)
          throw "Index should be greater than 0";
      
      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @kitfox
      If you use expections you will also need to catch it at some point. Otherweise your program exits for an uncaught expection.
      I am not a big fan of exceptions. You should rather design your program to handle the invalid state properly by your software design.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      2
      • K kitfox

        I come from a Java background and am used to throwing an exception when I check something and find it has an invalid value. I tend to use them much like assert statements - I'm just checking to make sure the program has not entered an invalid state, and if it has I want the program to halt at that point and print an error message. I rarely catch thrown exceptions since usually they represent faulty behavior and there is little point in continuing.

        What would be the best way to do something similar in Qt/C++? I've tried adding in throw clauses, but they cause my program to exit without printing an error message:

        if (index < 0)
            throw "Index should be greater than 0";
        
        E Offline
        E Offline
        elfring
        wrote on last edited by
        #3

        I've tried adding in throw clauses, but they cause my program to exit without printing an error message:

        I suggest to take another look into corresponding information sources like the article “Quality Matters #6: Exceptions for Practically-Unrecoverable Conditions”.

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved