Alternatives to exceptions for error handling
Jacob Carlborg
doob at me.com
Mon Nov 30 12:59:32 UTC 2020
On Monday, 30 November 2020 at 11:43:01 UTC, Ola Fosheim Grøstad
wrote:
> But they are also a bit too simple. Like, if accessing external
> resources you often want to do retries. Annoying to throw all
> the way out.
>
> Consider for instance if you try to fetch a file from an url,
> then it fails. It would have been nice to inject a recovery
> handler that can analyze the failure and provide a new url,
> sleep then retry etc.
>
> e.g. something along the lines of this sketch:
>
> fetch_url(url) {
> retry with (url) {
> …download attempt…
> …throw http_fail, server_busy…
> } catch (…){
> …ok cleanup, nobody wanted a retry…
> }
> }
>
>
> main(){
> on http_fail(url){
> url = replace_with_backup_server(url)
> return true; // retry
> }
> data = fetch_url(url)
> …
> }
In Ruby there's the `retry` keyword for this. Although you cannot
control the recovery handler from the outside, like in your
example. In D terms you would put `retry` in a `catch` block and
it would run the code inside the `try` block again.
--
/Jacob Carlborg
More information about the Digitalmars-d
mailing list