Distinguish Business Exceptions from Technical

Distinguish Business Exceptions from Technical

Dan Bergh Johnsson

THERE ARE BASiCALLY TWO REASONS that things go wrong at runtime: technical problems that prevent us from using the application and business logic that prevents us from misusing the application. Most modern languages, such as LISP, Java, Smalltalk, and C#, use exceptions to signal both these situa- tions. However, the two situations are so different that they should be carefully held apart. It is a potential source of confusion to represent them both using the same exception hierarchy, not to mention the same exception class.

An unresolvable technical problem can occur when there is a programming error. For example, if you try to access element 83 from an array of size 17, then the program is clearly off track, and some exception should result. The subtler version is calling some library code with inappropriate arguments, causing the same situation on the inside of the library.

It would be a mistake to attempt to resolve these situations you caused your- self. Instead, we let the exception bubble up to the highest architectural level and let some general exception-handling mechanism do what it can to ensure that the system is in a safe state, such as rolling back a transaction, logging and alerting administration, and reporting back (politely) to the user.

A variant of this situation is when you are in the “library situation” and a caller has broken the contract of your method, e.g., passing a totally bizarre argu- ment or not having a dependent object set up properly. This is on a par with accessing the 83rd element from 17: the caller should have checked; not doing so is a programmer error on the client side. The proper response is to throw a technical exception.

??42 97 Things Every Programmer Should Know

?

???????????????A different, but still technical, situation is when the program cannot proceed because of a problem in the execution environment, such as an unresponsive database. In this situation, you must assume that the infrastructure did what it could to resolve the issue—repairing connections and retrying a reasonable num- ber of times—and failed. Even if the cause is different, the situation for the calling code is similar: there is little it can do about it. So, we signal the situation through an exception that we let bubble up to the general exception-handling mechanism.

In contrast to these, we have the situation where you cannot complete the call for a domain-logical reason. In this case, we have encountered a situation that is an exception, i.e., unusual and undesirable, but not bizarre or programmati- cally in error (for example, if I try to withdraw money from an account with insufficient funds). In other words, this kind of situation is a part of the con- tract, and throwing an exception is just an alternative return path that is part of the model and that the client should be aware of and be prepared to handle. For these situations, it is appropriate to create a specific exception or a separate exception hierarchy so that the client can handle the situation on its own terms.

Mixing technical exceptions and business exceptions in the same hierarchy blurs the distinction and confuses the caller about what the method contract is, what conditions it is required to ensure before calling, and what situations it is supposed to handle. Separating the cases gives clarity and increases the chances that technical exceptions will be handled by some application framework, while the business domain exceptions actually are considered and handled by the client code.

时间: 2024-08-28 15:13:01

Distinguish Business Exceptions from Technical的相关文章

ARTS Week 11

Jan 6, 2020 ~ Jan 12, 2020 Algorithm Problem 108 Convert Sorted Array to Binary Search Tree (将有序数组转化为二叉搜索树) 题目链接 题目描述:给定一个有序数组,将其转换为一个高度平衡的二叉搜索树.高度平衡:对于树中任一结点,其左子树和右子树的高度不大于1.二叉搜索树:对于树中任一结点,其左子树所有结点的值小于等于该结点的值,其右子树所有结点的值大于等于该结点的值.例如给定数组为 [-10,-3,0,5,

Project Management Process

Project Management ProcessDescription .......................................................................................................................................................................................1STAGE/STEP/TASK SUMMARY LIST

微软职位内部推荐-SDE II

微软近期Open的职位: Title: Software Development Engineer Location: Shanghai JOB DESCRIPTION: Be a part of MSIT Engineering Community & Online (ECO) Business Intelligence team to transform CSS to a data driven organization! &nbsp In this role, you'll buil

Spring框架文档与API(4.3.6版本)

http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/ Table of Contents I. Overview of Spring Framework 1. Getting Started with Spring 2. Introduction to the Spring Framework 2.1. Dependency Injection and Inversion of Contr

领域驱动设计 - 在你开始之前清理一下你的观念

-------------------------------------之前园子里面有人翻译过,在家闲的没事又整理了一遍. Start Developing a New Application 开始一个新的应用程序 What we traditionally do when we start a business application? We read the spec and find the functionalities. We break down tasks. In most of

UML期末复习题——2.6:Package Diagram

第六题 包图 重要概念: 1.包图(package Diagram) 由若干个包以及包之间的关系组成.包是一种分组机制,其将一些相关的类集合为一个包,形成高内聚,低耦合的类集合,可以说,一个包相当于一个子系统. 2.简要介绍: 包图是一种维护和描述系统总体结构的模型的重要建模工具,通过对包中各个包以及包之间关系的描述,展现出系统的模块与模块之间的依赖关系.在面向对象软件开发的视角中,类显然是构建整个系统的基本构造块.但是对于庞大的应用系统而言,其包含的类将是成百上千,再加上其间“阡陌交纵”的关联

SOA宣言和微服务特点

如果从概念层来看,我更喜欢把SOA归为企业架构的范畴,从企业架构出发把业务分解为不同业务域的服务,关注系统间的服务互联互通的规范,并不关心如何实现.也就是说在企业架构上使用SOA支撑业务,而在方案架构使用微服务架构来实现. . SOA 宣言 面向服务是一种规范行为的范式.面向服务架构(SOA)是一种应用于面向服务而形成的一种架构. 我们一直以来运用面向服务来帮助组织始终如一的交付可持续的业务价值, 以提高灵活性和成本效益来符合变化的业务需求. 在我们的工作中, 我们会作如下优先排序: 业务价值

ABAP Netweaver, SAP Cloud Platform和Kubernetes的用户区分

ABAP Dialog: Individual, interactive system access. System: Background processing and communication within a system (such as RFC users for ALE, Workflow, TMS, and CUA). Communication:Dialog-free communication for external RFC calls. Service: Dialog u

Tell Me About Yourself - Best Answers and Examples

http://a4academics.com/interview-questions/73-human-resource/723-tell-me-about-yourself?showall=&start=2 Experienced – Front End Web Developer/Designer (3-6 Years) Minimum Qualification: Bachelor's Degree Industry: IT-Software Key skills: hand-on exp