Home > Software engineering >  Is there a way to handle exeption without try catch block?
Is there a way to handle exeption without try catch block?

Time:10-24

Sorry for all mistakes, English is not my native language. I have code that contains following lines:

tf::TransformListener listener;
tf::StampedTransform transform;
while(ros.ok()) {
try {
    listener.lookupTransform("/map", "/base_link", ros::Time(0), transform);
    } catch(tf::TransformException ex) {
    //ROS_ERROR("Transform problem -> %s", ex.what());
 }

The problem is I should achieve same effect without try catch block. I need to use this code on arduino, and I have hard times understanding is it possible to achieve same result with if else statements? Appreciate any help.

CodePudding user response:

As Sam Varshavchik put it,

If an exception gets thrown in a C program, there only way to catch it and resume execution, at some point, is a try/catch block. This is the only way to do it, there are no workarounds or exceptions.

As this answer said, the only way for a global error handler is to put the entire program into a try/catch statement, or at least the part that would cause an error. Even better, you can make sure your program just doesn't throw an error in the first place, by checking values and asserting.

But all in all, no, there is no way to do this.

  • Related