From 7550ae5221480f56fa55294bc1d9c8f8bc74591a Mon Sep 17 00:00:00 2001 From: Joseph Verron Date: Tue, 17 Sep 2024 10:54:41 +0800 Subject: [PATCH] Add sneakyThrow method to OfficeStamperException class This method encapsulates and throws provided exceptions as an OfficeStamperException. It improves error handling by standardizing the way unexpected errors are thrown and logged. This addition also enhances code readability and maintainability. --- .../officestamper/api/OfficeStamperException.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/engine/src/main/java/pro/verron/officestamper/api/OfficeStamperException.java b/engine/src/main/java/pro/verron/officestamper/api/OfficeStamperException.java index ddab13f0..7993eeef 100644 --- a/engine/src/main/java/pro/verron/officestamper/api/OfficeStamperException.java +++ b/engine/src/main/java/pro/verron/officestamper/api/OfficeStamperException.java @@ -2,8 +2,7 @@ /** * OfficeStamperException is a subclass of RuntimeException that represents an exception that can be thrown during the - * processing of an Office document using the OfficeStamper library - * . + * processing of an Office document using the OfficeStamper library. * It provides additional constructors to handle different scenarios. */ public class OfficeStamperException @@ -51,4 +50,15 @@ public OfficeStamperException(String message, Throwable cause) { public OfficeStamperException() { super("Unexpected exception"); } + + /** + * Throws an OfficeStamperException encapsulating the provided exception. + * + * @param exception the exception to be thrown + * @return this method does not return a value; it always throws an exception + * @throws OfficeStamperException always thrown to indicate an unexpected error. + */ + public static T sneakyThrow(Exception exception) { + throw new OfficeStamperException("Unexpected error", exception); + } }