1

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.

sempaiscuba
1,0429 gold badges21 silver badges32 bronze badges
asked Nov 27, 2022 at 9:41
3
  • 2
    This try/catch construct won't work with Arduino C++ : forum.arduino.cc/t/try-catch/180063 Commented Nov 27, 2022 at 9:57
  • OK. There is little more to say so I'll present that as the answer. Commented Nov 27, 2022 at 14:40
  • Besides that, why do you think that the NTP client is going to throw an exception? Commented Nov 27, 2022 at 17:34

2 Answers 2

1

You might want to refresh your knowledge about the try-catch statement by reading the respective chapter in your C++ book.

You need to declare an exception parameter in catch, which you can ignore:

 try {
 timeClient.update();
 ntp_seconds = timeClient.getSeconds();
 ntp_minutes = timeClient.getMinutes();
 ntp_hours = timeClient.getHours();
 } 
 catch (exception& ignored) {
 ntp_seconds = 0;
 ntp_minutes = 0;
 ntp_hours = 0;
 }
answered Nov 28, 2022 at 10:20
0

The Arduino IDE does not support the C++ try/catch exception handling construct and explicitly disables this feature. See: https://forum.arduino.cc/t/try-catch/180063 for a fuller explanation.

answered Nov 27, 2022 at 14:38
1
  • how does IDE disable it? the build commands are in platform.txt of each platform. a forum post from 2013 is maybe outdated Commented Nov 27, 2022 at 15:29

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.