Try/ catch not working in sendSync urlconnection

i’m trying try/catch with url connection.sendSync
when it bumps into an unvalid url, it doesn’t catch the error ?

sorry i’m not used to try/catch, i used error codes, try to upgrade my code

You are debugging with ‘break on exceptions’?, if you resume you will see that the next line where the IDE stops is the catch area.

3 Likes

i don’t know lol
is this in preferences ? i can’t find it, thanks

image

1 Like

Keep in mind that with this setting turned off, your app will just crash if it encounters an uncaught exception in debug mode,

A better solution would be to write your code so as to prevent the exception at all and the use exception handling to deal with the truly unexpected and “exceptional” situations.

I also feel that I should point out (based on the code snippet that you posted) that the html that you get with a URLConnection will not be the same as a browser or HTMLViewer. URLConnection has no JavaScript engine, CSS Model or Document Object Model to work with so any post-load manipulations that is often done these days would not be made in the HTML you get back using this method.

I’m sorry if my post suggested removing the ‘break on exceptions’ check is the recommended approach.

Maybe this is better: “that is the expected behavior while you are debugging because by default Xojo breaks on exceptions when you debug, which helps when you are fine-tuning your code to avoid that. If you use try-catch simple Resume to continue debugging. If you want to avoid the ‘break on exceptions’ you can momentarily change that while you test your code”.

@AlbertoD - I did not mean to imply that. I just did not want him to be surprised.

1 Like

Or if you don’t want it to break on THAT exception while debugging but leave the setting enabled so it breaks elsewhere, you can do this:

#Pragma BreakOnExceptions False
(your code here which you don't want to break on exceptions during debug)
#Pramga BreakOnExceptions Default

Many years ago I used “True” instead of “Default” at the end. But this forces the setting back on where “Default” will make it revert to your current IDE setting. And you don’t need this #Pragma at all if the code will be going out of scope. But I tend to always pair them together so if I add code later it will break again if appicable.

1 Like