You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Generally, it is a good idea to model Application-Specific Errors using Type Hierarchies (ADTs) and leaving exceptions for Programming Errors. Creating an exception for a simple error can be inefficient, as it includes a full stack trace, and exceptions are meant to remain uncaught to cause a crash and highlight critical programming mistakes.
Roman Elizarov has written a great article about Kotlin and Exceptions that is worth reading.
Additionally, using type hierarchies makes APIs more discoverable. For instance, you would check for BazaarIsNotInstalledException only if you know it exists. However, with type hierarchies, you can't easily miss that cause the compiler will scream at you :)
Since exception messages cannot be used directly without localization, you might end up treating all errors the same and miss the opportunity of already knowing root cause of the error, such as BazaarIsNotInstalled.
// ExceptionsBazaarUpdater.getLastUpdateState(context) { result ->when (result) {
/* ... */isError-> {
// Can't use non-localized `result.message`
showMessage("خطا در بروزرسانی از بازار")
}
}
}
// Type HierarchyBazaarUpdater.getLastUpdateState(context) { result ->when (result) {
/* ... */// Compiler added this branchisError.BazaarIsNotInstalled-> {
showMessage("اپلیکیشن بازار روی دستگاه شما یافت نشد")
}
isError.Unknown-> {
showMessage("خطا در بروزرسانی از بازار")
}
}
}
The text was updated successfully, but these errors were encountered:
alibagherifam
changed the title
Modeling Error results with type hierarchy
[Feature Request] Modeling Error results using type hierarchy
Sep 25, 2024
Thank you for bringing this issue to our attention. We appreciate your insight and agree that your suggestion could be quite beneficial. If you'd like, please feel free to submit a pull request with your proposed fix. We'd be happy to review it and work towards improving the project together. Thank you once again for your valuable contribution!
Generally, it is a good idea to model Application-Specific Errors using Type Hierarchies (ADTs) and leaving exceptions for Programming Errors. Creating an exception for a simple error can be inefficient, as it includes a full stack trace, and exceptions are meant to remain uncaught to cause a crash and highlight critical programming mistakes.
Roman Elizarov has written a great article about Kotlin and Exceptions that is worth reading.
Additionally, using type hierarchies makes APIs more discoverable. For instance, you would check for
BazaarIsNotInstalledException
only if you know it exists. However, with type hierarchies, you can't easily miss that cause the compiler will scream at you :)Since exception messages cannot be used directly without localization, you might end up treating all errors the same and miss the opportunity of already knowing root cause of the error, such as
BazaarIsNotInstalled
.The text was updated successfully, but these errors were encountered: