019-004-012

Exception Translation Pattern

Hard

Problem Description

Exception Translation Pattern

In this problem: You will implement the exception translation pattern by catching a low-level exception (NumberFormatException) and converting it to a business-appropriate exception (IllegalArgumentException).

Learning Objective: Understand how to appropriately convert exceptions between layers using the exception translation pattern

Overview

In layered architectures, exposing low-level implementation exceptions directly to upper layers is poor practice. The exception translation pattern catches low-level exceptions and converts them to exceptions appropriate for the business logic context. The original exception is preserved as the cause.

Specifications

  • OrderService class: processOrder(String quantityStr, String priceStr) method
  • Convert NumberFormatException to IllegalArgumentException on number conversion failure
  • Include business context in the translated exception message
  • Set the original exception as cause
  • Call from Main and print normal and error cases

Output Format

Order total: 1500
Business error: Invalid order quantity: not_a_number
Caused by: java.lang.NumberFormatException

Ready to Try Running Code?

Log in to access the code editor and execute your solutions for this problem.

Don't have an account?

Sign Up