Exceptions
Technicaltitle: Exceptions created_at: 2021-08-25T14:52:16.823Z updated_at: 2021-08-25T14:52:16.823Z tldr: Return, catch, crash is_published: 1 star: false category: Technical share_type: add
All software is a reflection of what matters and what doesn't matter. Both need to be treated equally.
If you have ever tried to clean up the .temp
dir in Windows, some of the files won't be deleted. Ah, screw it, restart the system and things should look fine, right? Nope, they were being used by the system. So When I manually tried to delete the files should it not throw an exception?. Ok, what Windows thinks if a file is open, it's being used and cannot be deleted.
In Unix, when a file is deleted, the file will be marked deleted right away, but the contents of the file remain till the last access is lost. No exceptions, just beautiful.
When designing software, how do I handle exceptions? Should it return the errors, throw exceptions, or straight away crash.
Returns
In most cases just return the error. Say in a web server there is a malformed request incoming. Just refuse to fulfill. It's not necessary to say what went wrong as long as the status code is returned, but it's easier to debug and nicer to the user using the service.
Exceptions
There is a process that needs a file read but the file is not there? Throw an exception. Exceptions are meant to be at the process level, that need attention from outside the normal flow of application context. Let the process manager or the master handle how it needs to handle it next. Re-spawn the child or short-circuit to another method or retry.
Crashing
Remember all the old games that went to a black screen and you don't know what the hell happened? It's just the game had a pointer to a frame/memory that needed to be accessed but the memory was cleaned. Now you had to break into the task manager, locate the daemon process, and force quit. Ah, it would be just easier if the program just crashed!. And its perfectly ok to crash systems some times than to handle exceptions and go into an infinite loop of resource hogging!
Having thoughts ?
Add yours