Try + Catch Macrooloop killing itself

Post anything you want to discuss with others about the software.

Try + Catch Macrooloop killing itself

Postby Parkgrr » Wed Sep 15, 2021 4:37 pm

Hey folks

I'm having an issue with a macroloop that is shutting itself down (meaning the macro finishes, then shuts down within UCCNC and stops looping).

Short of posting the entire macro I will say that it has everything to do with try + catch combos. Every time I do a try and an exception is thrown it will execute the 'catch' code then rather than restarting the macro it just get's 'killed', stops looping, and isn't running when I check on it in macroloop window.

Has anyone else experienced this? It's happening with every try + catch I do, no matter what they are doing. So for example:

double GetDouble(string TempString){
try {
TempDouble = Convert.ToDouble(TempString);
return TempDouble;
}
catch(FormatException ex) {
MessageBox.Show("Caught exception in GetDouble method.");
return;
}
}

In this example the "Caught exception" messagebox will show, then as soon as I click 'ok' the macro shuts down and stops looping. If I ask it to show a messagebox in the parent method AFTER the method is called it will not. The whole thing breaks at that last 'return' in the catch (whether I tell the method to return a value or not). Is there some system exception handling within UCCNC I should know about? Looking to have this macroloop catch exceptions, handle them, then continue looping.

Best
Parkgrr
 
Posts: 98
Joined: Mon Dec 07, 2020 9:12 am

Re: Try + Catch Macrooloop killing itself

Postby Parkgrr » Thu Sep 23, 2021 3:37 am

Has anyone had success with Try + Catches in UCCNC macroloops? Is there some other way to work with system exceptions I’m missing?
Parkgrr
 
Posts: 98
Joined: Mon Dec 07, 2020 9:12 am

Re: Try + Catch Macrooloop killing itself

Postby dezsoe » Thu Sep 23, 2021 12:30 pm

I didn't test the try-catch. However, if you want to convert then use the following:

Code: Select all
double GetDouble(string TempString)
{
  double TempDouble;
  if (!Double.TryParse(TempString, out TempDouble))
    Console.WriteLine("Conversion failed.");
  return TempDouble;
}

The .TryParse methods are very useful, because you won't get an error if the conversion fails, but its result is false. If the conversion fails then the variable (TempDouble in the example) will get its default value, now 0.0 for double.
dezsoe
 
Posts: 2049
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary

Re: Try + Catch Macrooloop killing itself

Postby dezsoe » Thu Sep 23, 2021 12:55 pm

I tested the try-catch, but had no problem. See video here.

The code for testing:

Code: Select all
Console.WriteLine("macroloop start");

double x = GetDouble("alma");
Console.WriteLine("x = " + x.ToString());

double y = GetDouble("1.12");
Console.WriteLine("y = " + y.ToString());

//loop = false; // uncomment to stop the loop

Console.WriteLine("macroloop finish");

#Events

double GetDouble(string TempString)
{
  try
  {
    double TempDouble = Convert.ToDouble(TempString);
    return TempDouble;
  }
  catch(FormatException ex)
  {
    MessageBox.Show("Caught exception in GetDouble method.");
    return 0.0;
  }
}
dezsoe
 
Posts: 2049
Joined: Sun Mar 12, 2017 4:41 pm
Location: Csörög, Hungary

Re: Try + Catch Macrooloop killing itself

Postby Parkgrr » Wed Oct 13, 2021 11:25 pm

Deszoe,

I can't thank you enough for taking the time! Thanks for making that video and taking such an in depth look.

Because of your answer I started looking elsewhere. I had a "return;" in the if statement that called the method I posted. I thought that a "return;" would start the macro over, but as I found out a return kills the macro loop and it doesn't start again. I removed the return and did some if else barriers instead and the macro now works great.

I really appreciate it.
Parkgrr
 
Posts: 98
Joined: Mon Dec 07, 2020 9:12 am


Return to General discussion about the UCCNC software

Who is online

Users browsing this forum: No registered users and 23 guests