A similar action to a try catch statement?
-
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
-
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
Set and get your boolean via a property, so you can raise an event when it is changed. Christian Graus - Microsoft MVP - C++
-
Set and get your boolean via a property, so you can raise an event when it is changed. Christian Graus - Microsoft MVP - C++
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
-
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
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++
-
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++
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
-
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
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.
-
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.
.... 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"; } }
-
.... 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"; } }
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
orApplicationException
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. -
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
orApplicationException
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.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.
-
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.
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).
-
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).