tomas-mrkvička
02/08/2022, 8:34 PMtry { } catch(e: Exception) { }
just to be sure you don't miss anything even if the bottom API change?Thanks.Michael de Kaste
02/08/2022, 8:46 PMTim Oltjenbruns
02/08/2022, 8:51 PMTim Oltjenbruns
02/08/2022, 8:52 PMTim Oltjenbruns
02/08/2022, 8:52 PMTim Oltjenbruns
02/08/2022, 8:53 PMTim Oltjenbruns
02/08/2022, 8:53 PMTim Oltjenbruns
02/08/2022, 8:55 PMOliver.O
02/08/2022, 9:48 PMJavier
02/08/2022, 10:57 PMtomas-mrkvička
02/12/2022, 10:16 AMTim Oltjenbruns:
If it’s reasonable for the consumer of a function to handle the error, don’t use an exception. If it’s unreasonable for the consumer of the function to handle the error, use an exception.Well, in case you want to use coroutines and their automatic cancellation, you are FORCED to use exceptions. So no
Either
or similar constructs to save the day.
So basically you all do not bother with exceptions. Which means that any kind of exceptions can go through you business service layer (I/O, DAO, SecurityException, 3rd party exceptions, ...) just like there are no boundaries between application layers.
And then what? You create numerous handlers (and manually specify all possible exceptions) to react accordingly? The problem goes bigger if you have multiple implementations of a service and each implementation can throw different exceptions.
How do you document such a service which can actually throw the whole universe of exceptions? Do you even document them? I do not like the idea that my service layer can throw any kind of exceptions without documenting what can go wrong. And wrapping underlying exceptions to some service-like exceptions doesn't fit to "not bother with exception".Tim Oltjenbruns
02/12/2022, 5:31 PMTim Oltjenbruns
02/12/2022, 5:45 PMTim Oltjenbruns
02/12/2022, 5:54 PMTim Oltjenbruns
02/12/2022, 5:55 PMtomas-mrkvička
02/12/2022, 6:45 PMAs a rule of thumb, you should not be catching exceptions in general Kotlin code. That’s a code smell. Exceptions should be handled by some top-level framework code of your application to alert developers of the bugs in the code and to restart your application or its affected operation. That’s the primary purpose of exceptions in Kotlin.and:
You should design your own general-purpose Kotlin APIs in the same way: use exceptions for logic errors, type-safe results for everything else. Don’t use exceptions as a work-around to sneak a result value out of a function.And many other statements. What is the point of throwing
IllegalArgumentException
from a business layer without any context? Next time you choose another implementation of your service interface and now an IOException
may be thrown from your business layer.
I would like to defining a strict description of my business layer with business-specific exceptions without leaking any lower ones but this require many try { } catch(LowLevelException) { /* throw BusinessException */ }
blocks which is a code-smell according to others.
So this is where my internal struggle with exceptions in Kotlin and disagreement with the article come from.Tim Oltjenbruns
02/12/2022, 6:56 PMOliver.O
02/16/2022, 3:05 PMSo basically you all do not bother with exceptions. Which means that any kind of exceptions can go through you business service layer (I/O, DAO, SecurityException, 3rd party exceptions, ...) just like there are no boundaries between application layers.Well, I do bother. Examples: • I handle I/O exceptions at the level where these are expected and can be dealt with appropriately. • I handle specific exceptions originating in the business infrastructure layers (concurrent access conflicts, for example). These are similar to I/O exceptions in the sense that they can be raised at multiple points in a complex sequence of actions, but can be dealt with in a centralized way. These exceptions are not simple wrappers of lower-level exceptions, but provide relevant information to users about effects and causes. And, of course, such exceptions are part of the originating layer's interface, and properly documented. • Then there is the rest: An unlimited universe of non-recoverable exceptions, errors of all kinds. These I catch, log and recover from at the highest level, possibly via a full shutdown and restart. Due to their unlimited nature and non-recoverability, they are not documented.
Tim Oltjenbruns
02/16/2022, 3:35 PMTim Oltjenbruns
02/16/2022, 3:36 PMMichael de Kaste
02/16/2022, 3:58 PMTim Oltjenbruns
02/16/2022, 4:07 PMTim Oltjenbruns
02/16/2022, 4:07 PMTim Oltjenbruns
02/16/2022, 4:07 PMTim Oltjenbruns
02/16/2022, 4:07 PM