Re: Exception handling
- Posted by ghaberek (admin) Feb 03, 2023
- 701 views
As I was waiting for the bus this morning I was wondering how I'd write my JS code without try/catch. It was an interesting gedanken. One place where it would be pretty hard to avoid is where my JS code, running under C# via ClearScript is connected to Selenium. Occasionally, the connection dies without warning. I can catch the death by something like
function isHeDead(driver) { try { var url = driver.Url; return false; catch (E) { return true; } }
Now this is all dotnet, something that AFAICT Euphoria and Phix aren't addressing, and so the connection technology doesn't have an analogue. But I do wonder if such things can happen in OE/P where something dies that's can't be detected without the test itself terminating the process.
In your example, you're fetching the URL property of the driver in order to determine the connection status, correct? If I were writing a Selenium library for Euphoria, I would do the same thing:
function isHeDead( atom driver ) -- Selenium_GetUrl() would return a sequence (the URL) -- or an atom (an error code, like ERR_TIMEDOUT). object url = Selenium_GetUrl( driver ) if atom( url ) then -- we received ERR_TIMEDOUT return FALSE end if -- we received "https://..." return TRUE end function
Now I'm not saying try/catch isn't a good thing, but in this example I don't see how the two methods are any different.
You could also try the Go approach, which also doesn't have exceptions and instead returns a value and error code from every function:
function isHeDead( atom driver ) -- Selenium_GetUrl() would always return a sequence containing two items: -- [1] a sequence: the URL (on success) or an empty sequence (on error) -- [2] an atom: the error code (on error) or zero (on success) atom err sequence url {url,err} = Selenium_GetUrl( driver ) if err then -- we received {"",ERR_TIMEDOUT} return FALSE end if -- we received {"https://...",ERR_OK} return TRUE end function
This approach is more strictly enforced in Go than it would be in Euphoria, but it provides good error handling without exceptions. The multiple assignment feature was added in Euphoria 4.1.
In any case, I'm not pushing for OE to have try/catch. I'll create the track as targeting Euphoria generally and then hand it over to the OE maintainers to bless or butcher as they see fit.
What are you talking about, Bruce? It's your baby! You'll need to raise maintain it yourself.
-Greg