URL Crash

If myURL = https://www.apple.com/osx/\\ is sent to a URLConnection
URLConnection1.Send(“GET”,myURL)

It crashes the my App. Most other invalid Urls I pick up in the URLConnection Event.
How can I intercept this one?

No surprise with extra backslashes in the end.

The other bad urls get caught in the urlconnection>Error event why not this one?

That’s probably a bug, it should be all to Error event or None (e.g. to InvalidArgumentException)

Put in a Feedback.

1 Like

In my opinion this is not a bug.
Bad URLs where the server does not exist will fire the Error event.

But trying to connect to a wrongly formed URL should raise an exception as is the case here.

1 Like

I disagree. Too many things in Xojo these days are being treated as exceptions instead of returning error codes or in this case firing an error event. This leads to code cluttered with many Try-Catch structures, when it’s much cleaner and less verbose to simply test a code or a returned value.

3 Likes

It doesn’t really matter if there is an exception or an error. What shouldn’t be there at all is an unhandled exception.

Can you do a regex to check if the url is malformed or not before trying to load it?

I disagree. For example, when a file open dialog returns, do you want to just check with one line if the returned file is nil, or have to wrap the whole thing in a Try-Catch? And remember or have to look up what kind of exception is involved.

IIRC, we used to have FolderItem.error, which you could test after doing something like a rename. Now you have to wrap it in a Try-Catch. There is a growing number of these cases, seems to be a trend.

IMO exceptions should be raised only when the framework truly has no way to deal with the condition, like NilObject, and not for everyday error reporting.

2 Likes

I think Beatrix meant it a little differently, but it was confusing that she used the term “exception” again at the end of her sentence. I think her point is that it is not good that something like this does not cause an “exception”, nor an “error” and that consequently you need to test it on your own (e.g. with a regex).

So she criticises, in my opinion rightly, that you can’t be sure that all possible errors are detected, but if necessary you have to add your own tests. It seems that we are all on the same page :wink: .

2 Likes

In the end I might use an offscreen HTMLViewer to test the URL via the Error and DocumentFinished events. It doesn’t crash the app even when the user puts in unwanted \.
:slight_smile:

you could just as well use regex or simply ReplaceAll("", “”)

Only problem with the incorrect urls was there were lots of characters that crashed the app

unless you catch the exception an tell the user it should enter something sane?

Try
MyConn.Send("GET", "http;?!pgf//'\\abadurl/hello")
Catch e As invalidArugmentException
MessageBox "Dear user, please enter a valid url and try again."
End Try
2 Likes
Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.