Passing variable into catch() exception

I'm working with a try{} catch(){} exception right now and I want to change a Variable. For this my board has to connect to the Internet and with a try{} statement it will try to do so. If it fails I simply want to set the variables to 0.

try {
    timeClient.update();
    ntp_seconds  = timeClient.getSeconds();
    ntp_minutes = timeClient.getMinutes();
    ntp_hours = timeClient.getHours();
  } catch () {
    ntp_seconds = 0;
    ntp_minutes = 0;
    ntp_hours = 0;
  }

I am getting this error: Compilation error: expected type-specifier before ')' token
I know that I somehow have to pass my variables into the catch() "function" but somehow I can't pass variables in like I would with normal functions.

Which arduino are you using?

actually I am using an esp8266

I’m not even sure try-catch is supported on the esp8266 nor if any of the function you call would raise an error

NTPClient::update() returns a boolean indicating if updating was successful (it will only attempt an update every updateInterval ms (every minute if you did not modify the default setting) and you will see false only when the function actually attempted to connect to the NTP server for an update, and that update failed. I think The time library will continue with the last synced time though

If you really want to force the data to 0 then do

  if (timeClient.update()) {
    ntp_seconds  = timeClient.getSeconds();
    ntp_minutes = timeClient.getMinutes();
    ntp_hours = timeClient.getHours();
  } else {
    ntp_seconds = 0;
    ntp_minutes = 0;
    ntp_hours = 0;
  }

you need to specify the Exception name, e.g.

} catch (exception& exc) {

if I try passing in variables like this:

catch (int ntp_seconds, int ntp_minutes, int ntp_hours)

I get this error:

Compilation error: exception handling disabled, use '-fexceptions' to enable

As I said try-catch is not supported by default and the function you call does not raise anything anyway

ok and what is the exception name ?

Read Answers…
Feels like I wasted my time

Bye

Which type of exceptions is timeClient supposed to throw?
That, or a base class of that exception, is the parameter for the catch

Was too late, so to repeat the above answers: if the methods of the timeClient's class do not throw exceptions, you can't catch anything at all.

ok I'll try that thanks

Well actually I don't want to force the data to zero. But this is supposed to be a clock and I want it to start counting from zero if there's no Internet connection the first time you start it.

what I don't know is what the NTP client will do if it has no internet the first time its started. If it won't absolutely break my code it doesn't matter.

then just initialise the time variables to zero and just update them if the call works

if (timeClient.update()) {
    ntp_seconds  = timeClient.getSeconds();
    ntp_minutes = timeClient.getMinutes();
    ntp_hours = timeClient.getHours();
  }

(that will update based on the clock of the MCU though)

so

if you want the clock to stay at 0 if time was never set

if (timeClient.isTimeSet() && timeClient.update()) {
    ntp_seconds  = timeClient.getSeconds();
    ntp_minutes = timeClient.getMinutes();
    ntp_hours = timeClient.getHours();
  }

you also have the forceUpdate() function you can call that will tell you false if the connection cannot be made.

and the function isTimeSet() to check if the time was ever set