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#
  4. A similar action to a try catch statement?

A similar action to a try catch statement?

Scheduled Pinned Locked Moved C#
question
11 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.
  • K Offline
    K Offline
    Kuira
    wrote on last edited by
    #1

    Is there a way where I can catch a condition say if a bool becomes false anytime in the code, it will catch that boolean, print out a failed message and exit? The only ways I can see this work: - Make a Thread to monitor the boolean. (Dont really want to do this as its not what I want to really do). - Use throws on anytime on the if statement (I got lots of if statement everytime I do a function that returns a boolean statement). Is there any other way like modify the try to catch the boolean instead of exceptions? Kuira

    C 1 Reply Last reply
    0
    • K Kuira

      Is there a way where I can catch a condition say if a bool becomes false anytime in the code, it will catch that boolean, print out a failed message and exit? The only ways I can see this work: - Make a Thread to monitor the boolean. (Dont really want to do this as its not what I want to really do). - Use throws on anytime on the if statement (I got lots of if statement everytime I do a function that returns a boolean statement). Is there any other way like modify the try to catch the boolean instead of exceptions? Kuira

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Set and get your boolean via a property, so you can raise an event when it is changed. Christian Graus - Microsoft MVP - C++

      K 1 Reply Last reply
      0
      • C Christian Graus

        Set and get your boolean via a property, so you can raise an event when it is changed. Christian Graus - Microsoft MVP - C++

        K Offline
        K Offline
        Kuira
        wrote on last edited by
        #3

        Sounds like a plan, but how do you end the the current method that this boolean is on? eg. Property B {get{read data} set{IF failure [???]}} Method A { [is there something I can do here to conditionalize if B is not true do something] B = test1; B = test2; [this is where it catches the condition when its not true] } -- modified at 19:26 Thursday 10th November, 2005

        C 1 Reply Last reply
        0
        • K Kuira

          Sounds like a plan, but how do you end the the current method that this boolean is on? eg. Property B {get{read data} set{IF failure [???]}} Method A { [is there something I can do here to conditionalize if B is not true do something] B = test1; B = test2; [this is where it catches the condition when its not true] } -- modified at 19:26 Thursday 10th November, 2005

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          Kuira wrote:

          how do you end the the current method that this boolean is on?

          Oh, OK. Why do you want to do that ? Surely there's better ways to do it ( for example, check the boolean in the method in question ). If you wanted, you could throw an exception here and put a try/catch in the method that sets it, but it's really convoluted, and ugly. If I get this right, your calling code will change a boolean, and if it sets it to the right value, the rest of that function won't execute ? Why do you need to do this ? Christian Graus - Microsoft MVP - C++

          K 1 Reply Last reply
          0
          • C Christian Graus

            Kuira wrote:

            how do you end the the current method that this boolean is on?

            Oh, OK. Why do you want to do that ? Surely there's better ways to do it ( for example, check the boolean in the method in question ). If you wanted, you could throw an exception here and put a try/catch in the method that sets it, but it's really convoluted, and ugly. If I get this right, your calling code will change a boolean, and if it sets it to the right value, the rest of that function won't execute ? Why do you need to do this ? Christian Graus - Microsoft MVP - C++

            K Offline
            K Offline
            Kuira
            wrote on last edited by
            #5

            I have a testing procedure that calls my other testing procedures. If one of them fails the test, I want to be able to catch it, store it as a message and pass it back to the originator. Writing the error and saying this test failed, please try again and telling him to recall this function when he is ready for his set of data to be tested again. That way, the originator will only need to call one function, I will pass him the message pass or fail, and everyone is happy. I dont like the idea of bool = test 1 if bool is true (return message) bool = test 2 if bool is true (return message) ... Imagine I have 100 tests

            L 1 Reply Last reply
            0
            • K Kuira

              I have a testing procedure that calls my other testing procedures. If one of them fails the test, I want to be able to catch it, store it as a message and pass it back to the originator. Writing the error and saying this test failed, please try again and telling him to recall this function when he is ready for his set of data to be tested again. That way, the originator will only need to call one function, I will pass him the message pass or fail, and everyone is happy. I dont like the idea of bool = test 1 if bool is true (return message) bool = test 2 if bool is true (return message) ... Imagine I have 100 tests

              L Offline
              L Offline
              Leslie Sanford
              wrote on last edited by
              #6

              Kuira wrote:

              I have a testing procedure that calls my other testing procedures.

              Have the testing procedures return a boolean value indicating success or failure (true or false). Then represent those procedures with delegates. Put the delegates into a collection or an array. From the main testing procedure, iterate through the collection invoking each delegate until one returns false. If one of the testing procedures fails, that is returns false, make note of which procedure it is (this could involve some extra work to associate some information with each procedure delegate, but it should be pretty straightforward), and return the appropriate error message. To be honest, I'm not entirely clear on what you're trying to do, so this suggestion is in part guesswork. Hope it helps.

              K 1 Reply Last reply
              0
              • L Leslie Sanford

                Kuira wrote:

                I have a testing procedure that calls my other testing procedures.

                Have the testing procedures return a boolean value indicating success or failure (true or false). Then represent those procedures with delegates. Put the delegates into a collection or an array. From the main testing procedure, iterate through the collection invoking each delegate until one returns false. If one of the testing procedures fails, that is returns false, make note of which procedure it is (this could involve some extra work to associate some information with each procedure delegate, but it should be pretty straightforward), and return the appropriate error message. To be honest, I'm not entirely clear on what you're trying to do, so this suggestion is in part guesswork. Hope it helps.

                K Offline
                K Offline
                Kuira
                wrote on last edited by
                #7

                .... I dont know how to explain it but I'll try again: - User clicks on a button after they written out a form with a set of data that has been validated on the data side. - The middleware looks at this and then validates the set of data provided by the GUI to determine if the data that has been inputted is valid to be stored in a database. - These set of tests range from testing the database connectivity, if any of the data inputted will conflict with the current set of data or even if the entry cannot be inserted because the exact same data exists. - If one of these tests fail because of those issues, it will stop the test, write a message back to the user to explain the fault, and let the user decide if they want to refix it or cancel the action. So in essence currently in pseudo code its string test(input data, input tablename, output conflictions) { bool = test1() //connect to database if (!bool) { return "cannot connect to database"; } bool = test2(tablename) // checks if the table is not locked at the moment if (!bool) { return "currently in session, please try again later"; } bool = test3(data, tablename) // checks if the data is already inserted if (!bool) {return "data already exists in database!"; } bool = test4(data, tablename, conflictions) //validates if we insert this data and hits a number of constraints if (!bool) {return "Conflictions has occured please look at the conflictions and try again"; } }

                L 1 Reply Last reply
                0
                • K Kuira

                  .... I dont know how to explain it but I'll try again: - User clicks on a button after they written out a form with a set of data that has been validated on the data side. - The middleware looks at this and then validates the set of data provided by the GUI to determine if the data that has been inputted is valid to be stored in a database. - These set of tests range from testing the database connectivity, if any of the data inputted will conflict with the current set of data or even if the entry cannot be inserted because the exact same data exists. - If one of these tests fail because of those issues, it will stop the test, write a message back to the user to explain the fault, and let the user decide if they want to refix it or cancel the action. So in essence currently in pseudo code its string test(input data, input tablename, output conflictions) { bool = test1() //connect to database if (!bool) { return "cannot connect to database"; } bool = test2(tablename) // checks if the table is not locked at the moment if (!bool) { return "currently in session, please try again later"; } bool = test3(data, tablename) // checks if the data is already inserted if (!bool) {return "data already exists in database!"; } bool = test4(data, tablename, conflictions) //validates if we insert this data and hits a number of constraints if (!bool) {return "Conflictions has occured please look at the conflictions and try again"; } }

                  L Offline
                  L Offline
                  Leslie Sanford
                  wrote on last edited by
                  #8

                  Ok, my eariler suggestion doesn't work because the test methods have different signatures. One approach would be to abandon using the boolean and just use exceptions:

                  public void Test(input data, input tablename, output conflictions)
                  {
                  Test1() //connect to database
                  Test2(tablename) // checks if the table is not locked at the moment
                  Test3(data, tablename) // checks if the data is already inserted
                  Test4(data, tablename, conflictions) //validates if we insert this data and hits a number of constraints
                  }

                  private void Test1()
                  {
                  // Do connection stuff...

                  if(unableToConntect)
                  {
                      throw new ApplicationException("Cannot connect to database.");
                  }
                  

                  }

                  You would probably want to use your own exception class derived from Exception or ApplicationException that contains information about the failure. If throwing an exception from the Test method is unacceptable, you could catch the exception in the method itself, and do whatever is appropriate there, or maybe even take some action and rethrow the exception.

                  K 1 Reply Last reply
                  0
                  • L Leslie Sanford

                    Ok, my eariler suggestion doesn't work because the test methods have different signatures. One approach would be to abandon using the boolean and just use exceptions:

                    public void Test(input data, input tablename, output conflictions)
                    {
                    Test1() //connect to database
                    Test2(tablename) // checks if the table is not locked at the moment
                    Test3(data, tablename) // checks if the data is already inserted
                    Test4(data, tablename, conflictions) //validates if we insert this data and hits a number of constraints
                    }

                    private void Test1()
                    {
                    // Do connection stuff...

                    if(unableToConntect)
                    {
                        throw new ApplicationException("Cannot connect to database.");
                    }
                    

                    }

                    You would probably want to use your own exception class derived from Exception or ApplicationException that contains information about the failure. If throwing an exception from the Test method is unacceptable, you could catch the exception in the method itself, and do whatever is appropriate there, or maybe even take some action and rethrow the exception.

                    K Offline
                    K Offline
                    Kuira
                    wrote on last edited by
                    #9

                    Ok, from the exception part, if Test1 fails from there, will that means that Test2 wont fire from the algorithm you written up? If so this should be acceptable, I'll just make a simple exception thats derived or something to say true or false, as long as I dont need to use if statements whenever I need to check each methods return value.

                    L 1 Reply Last reply
                    0
                    • K Kuira

                      Ok, from the exception part, if Test1 fails from there, will that means that Test2 wont fire from the algorithm you written up? If so this should be acceptable, I'll just make a simple exception thats derived or something to say true or false, as long as I dont need to use if statements whenever I need to check each methods return value.

                      L Offline
                      L Offline
                      Leslie Sanford
                      wrote on last edited by
                      #10

                      Kuira wrote:

                      Ok, from the exception part, if Test1 fails from there, will that means that Test2 wont fire from the algorithm you written up?

                      Right. Say you did something like this, building on the code example I provided in the previous post:

                      public void SomeMethod()
                      {
                      try
                      {
                      Test();
                      }
                      catch(TestException ex)
                      {
                      // Notify user that a failure occurred (if we're using a form,
                      // we might display a message box instead).
                      Console.WriteLine(ex.Message);
                      }
                      }

                      When an exception gets thrown from one of Test methods, flow of control jumps immediately to the catch block above without executing any of later Test methods.

                      Kuira wrote:

                      I'll just make a simple exception thats derived or something to say true or false, as long as I dont need to use if statements whenever I need to check each methods return value.

                      You can include an error message with the exception. Since each Test method knows what its purpose is, by having them throw an exception upon failure, they know also know what the error message should be (see my example in the previous post).

                      K 1 Reply Last reply
                      0
                      • L Leslie Sanford

                        Kuira wrote:

                        Ok, from the exception part, if Test1 fails from there, will that means that Test2 wont fire from the algorithm you written up?

                        Right. Say you did something like this, building on the code example I provided in the previous post:

                        public void SomeMethod()
                        {
                        try
                        {
                        Test();
                        }
                        catch(TestException ex)
                        {
                        // Notify user that a failure occurred (if we're using a form,
                        // we might display a message box instead).
                        Console.WriteLine(ex.Message);
                        }
                        }

                        When an exception gets thrown from one of Test methods, flow of control jumps immediately to the catch block above without executing any of later Test methods.

                        Kuira wrote:

                        I'll just make a simple exception thats derived or something to say true or false, as long as I dont need to use if statements whenever I need to check each methods return value.

                        You can include an error message with the exception. Since each Test method knows what its purpose is, by having them throw an exception upon failure, they know also know what the error message should be (see my example in the previous post).

                        K Offline
                        K Offline
                        Kuira
                        wrote on last edited by
                        #11

                        Ok sounds like a good plan to me, I'll use that thanks.

                        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