Translating Exceptions Example
This program demonstrates exception translation.
Problem Statement
Write a Java program that translates low-level exceptions into meaningful custom exceptions.
Source Code
| 1 | class BusinessException extends Exception { |
| 2 | public BusinessException(String msg){ super(msg); } |
| 3 | } |
| 4 | public class ExceptionTranslationExample { |
| 5 | public static void main(String[] args) { |
| 6 | System.out.println("Eduinq Exception Translation Example"); |
| 7 | try { |
| 8 | riskyOperation(); |
| 9 | } catch(BusinessException e) { |
| 10 | System.out.println("Eduinq Exception: "+e.getMessage()); |
| 11 | } |
| 12 | } |
| 13 | static void riskyOperation() throws BusinessException { |
| 14 | try { |
| 15 | int result=10/0; |
| 16 | } catch(ArithmeticException e) { |
| 17 | throw new BusinessException("Eduinq Business Rule Violated"); |
| 18 | } |
| 19 | } |
| 20 | } |
Program Output
Eduinq Exception Translation Example Eduinq Exception: Eduinq Business Rule Violated
Explanation
Exception translation converts technical errors into business-level exceptions. - Improves clarity for end-users and developers. - Provides meaningful context for errors. - In enterprise systems, translation ensures domain-specific error reporting. Conclusion: Shows exception translation.