• Что бы вступить в ряды "Принятый кодер" Вам нужно:
    Написать 10 полезных сообщений или тем и Получить 10 симпатий.
    Для того кто не хочет терять время,может пожертвовать средства для поддержки сервеса, и вступить в ряды VIP на месяц, дополнительная информация в лс.

  • Пользаватели которые будут спамить, уходят в бан без предупреждения. Спам сообщения определяется администрацией и модератором.

  • Гость, Что бы Вы хотели увидеть на нашем Форуме? Изложить свои идеи и пожелания по улучшению форума Вы можете поделиться с нами здесь. ----> Перейдите сюда
  • Все пользователи не прошедшие проверку электронной почты будут заблокированы. Все вопросы с разблокировкой обращайтесь по адресу электронной почте : info@guardianelinks.com . Не пришло сообщение о проверке или о сбросе также сообщите нам.

The exception translation pattern

Lomanu4 Оффлайн

Lomanu4

Команда форума
Администратор
Регистрация
1 Мар 2015
Сообщения
1,481
Баллы
155
? TL;DR


The exception translation pattern:

  • Cleans up APIs
  • Enforces robust error handling
  • Hides internal complexity
  • Aligns code with domain logic

Since I have been writing about exceptions lately, I figured it would be a good idea to elaborate on this, give it a name, a title and a cover image, and put together a nice post with code examples and diagrams, and list all the benefits that it brings to the table. In a future post, I'll give my opinion on areas of improvement.

The I/O exception example


A typical case of exception handling is when dealing with I/O methods. Let's look at how the exception translation pattern applies. It usually involves three (or sometimes more) tiers, where a "translation" or conversion takes place from one tier to next. The tiers can be described as follows:

  1. Library Tier (Low-Level I/O): A method like Files.readAllBytes() may throw an IOException if the file is missing or unreadable.
  2. Application Tier (Your Method): Your method catches this low-level exception and wraps it in a custom application-specific exception, such as ConfigLoadException, providing a clear, meaningful message.
  3. Caller Tier (Client Code): The caller interacts only with the application-level API. It does not need to understand IOException—only that configuration loading failed in a specific, well-defined way.
Visual diagram


Here’s a diagram showing the flow of control and exceptions:


+----------------+ +-----------------------+ +---------------------+
| Tier 1 | | Tier 2 | | Tier 3 |
| Low-Level I/O | ----> | Application Logic | ----> | Application Caller |
| (readFromFile) | throws | (loadConfig) | throws | (init) |
| IOException | | ConfigLoadException | | handles ConfigLoadEx |
+----------------+ +-----------------------+ +---------------------+
^ |
| |
+------ wraps IOException -------+
Example code


See below an example code illustrating this pattern:


// Custom application exception
public class ConfigLoadException extends Exception {
public ConfigLoadException(String message, Throwable cause) {
super(message, cause);
}
}

// Tier 1: Library
public String readFromFile(String path) throws IOException {
return new String(Files.readAllBytes(Paths.get(path)));
}

// Tier 2: Application method
public String loadConfig() throws ConfigLoadException {
try {
return readFromFile("config.txt");
} catch (IOException e) {
throw new ConfigLoadException("Failed to load configuration file.", e);
}
}

// Tier 3: Caller
public void init() {
try {
String config = loadConfig();
// proceed with config
} catch (ConfigLoadException e) {
// handle or log application-level error
}
}
✅ Why Exception Translation Is a Best Practice in Java


The exception translation pattern—catching a low-level exception and rethrowing a higher-level, domain-specific exception—is a powerful tool in layered application design. It allows developers to write cleaner, more maintainable code.

Here is the long list of benefits that make it a best practice:

? 1. Encapsulation of Implementation Details


You hide technical concerns (e.g., IOException, SQLException) from callers. They don’t need to know how your method works internally—just that something relevant to them failed.

Benefit: Promotes modularity and separation of concerns.
? 2. Improved Clarity and Context in Error Messages


Low-level exceptions are often vague. By translating them, you can provide business-relevant, human-readable messages that make debugging and logging easier.

Benefit: Easier troubleshooting and more meaningful error messages.
? 3. Consistent Error Handling API


You can map all internal errors to a predictable set of application-level exceptions. This simplifies the mental model for client developers.

Benefit: Reduced cognitive load and cleaner public APIs.
? 4. Stronger Compile-Time Guarantees


When your translated exceptions are checked exceptions, the compiler enforces proper handling by the caller.

Benefit: Prevents accidental omission of error handling.
? 5. Flexibility to Change Internal Implementations


Whether you switch from file-based storage to a database or change the library you use, your external API stays the same if you're translating exceptions properly.

Benefit: Future-proofing and easier refactoring.
?️ 6. Encourages Domain-Driven Design


Instead of leaking infrastructure terms, you use exceptions like ConfigLoadException or OrderProcessingException that reflect your business logic.

Benefit: Your code speaks the language of your domain.

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

 
Вверх Снизу