Alternatives to exceptions for error handling

Ola Fosheim Grøstad ola.fosheim.grostad at gmail.com
Mon Nov 30 11:43:01 UTC 2020


On Monday, 30 November 2020 at 11:02:29 UTC, Ali Çehreli wrote:
> That's all... Exceptions are the simplest error management. For 
> me, their only problem is the performance impact, which I 
> haven't even measured. :)

They are quite simple. The performance impact is simply because 
of a clumsy generic solution with expensive lookup.

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)
   …
}




More information about the Digitalmars-d mailing list