Skip to content

io microsphere util ExceptionUtils

github-actions[bot] edited this page Mar 21, 2026 · 1 revision

ExceptionUtils

Type: Class | Module: microsphere-java-core | Package: io.microsphere.util | Since: 1.0.0

Source: microsphere-java-core/src/main/java/io/microsphere/util/ExceptionUtils.java

Overview

Exception Utilities class

Declaration

public abstract class ExceptionUtils implements Utils

Author: Mercy

Version Information

  • Introduced in: 1.0.0
  • Current Project Version: 0.1.10-SNAPSHOT

Version Compatibility

This component is tested and compatible with the following Java versions:

Java Version Status
Java 8 ✅ Compatible
Java 11 ✅ Compatible
Java 17 ✅ Compatible
Java 21 ✅ Compatible
Java 25 ✅ Compatible

Examples

Method Examples

getStackTrace

try {
    // some code that may throw an exception
} catch (Exception e) {
    String stackTrace = ExceptionUtils.getStackTrace(e);
    System.out.println(stackTrace);
}

wrap

try {
    // some code that throws an IOException
} catch (IOException e) {
    RuntimeException re = ExceptionUtils.wrap(e, RuntimeException.class);
    throw re;
}
try {
    // some code that throws an IllegalArgumentException
} catch (IllegalArgumentException e) {
    MyCustomException myEx = ExceptionUtils.wrap(e, MyCustomException.class);
    throw myEx;
}

create

try {
    // some code that may throw an exception
} catch (Exception e) {
    RuntimeException re = ExceptionUtils.create(RuntimeException.class);
    throw re;
}

create

try {
    // some code that may throw an exception
} catch (Exception e) {
    RuntimeException re = ExceptionUtils.create(RuntimeException.class, "An error occurred");
    throw re;
}

create

try {
    // some code that may throw an exception
} catch (Exception e) {
    RuntimeException re = ExceptionUtils.create(RuntimeException.class, e);
    throw re;
}

create

try {
    // some code that may throw an exception
} catch (Exception e) {
    RuntimeException re = ExceptionUtils.create(RuntimeException.class, "An error occurred", e);
    throw re;
}

create

try {
    // some code that may throw an exception
} catch (Exception e) {
    RuntimeException re = ExceptionUtils.create(
        RuntimeException.class,
        e,
        "An error occurred with code: %d",
        500
    );
    throw re;
}

create

try {
    // some code that may throw an exception
} catch (Exception e) {
    RuntimeException re = ExceptionUtils.create(RuntimeException.class, "Error", e);
    throw re;
}
try {
    // some code that may throw an exception
} catch (Throwable t) {
    IOException ioEx = ExceptionUtils.create(IOException.class, "File not found");
    throw ioEx;
}

throwTarget

try {
    // some code that throws an IOException
} catch (IOException e) {
    throw ExceptionUtils.throwTarget(e, RuntimeException.class); // Rethrows as RuntimeException
}
try {
    // some code that throws an IllegalArgumentException
} catch (IllegalArgumentException e) {
    throw ExceptionUtils.throwTarget(e, MyCustomException.class); // Wraps and throws as MyCustomException
}

Usage

Maven Dependency

Add the following dependency to your pom.xml:

<dependency>
    <groupId>io.github.microsphere-projects</groupId>
    <artifactId>microsphere-java-core</artifactId>
    <version>${microsphere-java.version}</version>
</dependency>

Tip: Use the BOM (microsphere-java-dependencies) for consistent version management. See the Getting Started guide.

Import

import io.microsphere.util.ExceptionUtils;

API Reference

Public Methods

Method Description
getStackTrace Gets the stack trace from a Throwable as a String.
wrap Wraps the given source exception into a new instance of the specified type.
create Creates a new instance of the specified Throwable class using its default constructor.
create Creates a new instance of the specified Throwable class with the provided detail message.
create Creates a new instance of the specified Throwable class with the provided cause.
create Creates a new instance of the specified Throwable class with the provided detail message and cause.
create Creates a new instance of the specified Throwable class with a formatted detail message and cause.
create Creates a new instance of the specified Throwable class using constructor arguments.
throwTarget Throws the given source exception wrapped into the specified target exception type.

Method Details

getStackTrace

public static String getStackTrace(Throwable throwable)

Gets the stack trace from a Throwable as a String.

The result of this method may vary by JDK version as this method uses Throwable#printStackTrace(java.io.PrintWriter). On JDK1.3 and earlier, the cause exception will not be shown unless the specified throwable alters printStackTrace.

Example Usage

`try {
    // some code that may throw an exception
` catch (Exception e) {
    String stackTrace = ExceptionUtils.getStackTrace(e);
    System.out.println(stackTrace);
}
}

wrap

public static <T extends Throwable, TT extends Throwable> TT wrap(T source, Class<TT> thrownType)

Wraps the given source exception into a new instance of the specified type.

If the source exception is already assignable to the target type, it is returned directly. Otherwise, a new instance of the target type is created using suitable constructor arguments derived from the source exception.

Example Usages

`try {
    // some code that throws an IOException
` catch (IOException e) {
    RuntimeException re = ExceptionUtils.wrap(e, RuntimeException.class);
    throw re;
}
}
`try {
    // some code that throws an IllegalArgumentException
` catch (IllegalArgumentException e) {
    MyCustomException myEx = ExceptionUtils.wrap(e, MyCustomException.class);
    throw myEx;
}
}

create

public static <T extends Throwable> T create(Class<T> throwableClass)

Creates a new instance of the specified Throwable class using its default constructor.

Example Usage

`try {
    // some code that may throw an exception
` catch (Exception e) {
    RuntimeException re = ExceptionUtils.create(RuntimeException.class);
    throw re;
}
}

create

public static <T extends Throwable> T create(Class<T> throwableClass, String message)

Creates a new instance of the specified Throwable class with the provided detail message.

Example Usage

`try {
    // some code that may throw an exception
` catch (Exception e) {
    RuntimeException re = ExceptionUtils.create(RuntimeException.class, "An error occurred");
    throw re;
}
}

create

public static <T extends Throwable> T create(Class<T> throwableClass, Throwable cause)

Creates a new instance of the specified Throwable class with the provided cause.

Example Usage

`try {
    // some code that may throw an exception
` catch (Exception e) {
    RuntimeException re = ExceptionUtils.create(RuntimeException.class, e);
    throw re;
}
}

create

public static <T extends Throwable> T create(Class<T> throwableClass, String message, Throwable cause)

Creates a new instance of the specified Throwable class with the provided detail message and cause.

Example Usage

`try {
    // some code that may throw an exception
` catch (Exception e) {
    RuntimeException re = ExceptionUtils.create(RuntimeException.class, "An error occurred", e);
    throw re;
}
}

create

public static <T extends Throwable> T create(Class<T> throwableClass, Throwable cause, String messagePattern, Object... args)

Creates a new instance of the specified Throwable class with a formatted detail message and cause.

This method formats the message using the provided pattern and arguments, then creates a new instance of the target Throwable type with both the formatted message and the cause.

Example Usage

`try {
    // some code that may throw an exception
` catch (Exception e) {
    RuntimeException re = ExceptionUtils.create(
        RuntimeException.class,
        e,
        "An error occurred with code: %d",
        500
    );
    throw re;
}
}

create

public static <T extends Throwable> T create(Class<T> throwableClass, Object... args)

Creates a new instance of the specified Throwable class using constructor arguments.

This method attempts to instantiate the target Throwable class by matching and using the provided constructor arguments. It is particularly useful when the exact combination of constructor parameters is known and needs to be dynamically applied.

Example Usages

`try {
    // some code that may throw an exception
` catch (Exception e) {
    RuntimeException re = ExceptionUtils.create(RuntimeException.class, "Error", e);
    throw re;
}
}
`try {
    // some code that may throw an exception
` catch (Throwable t) {
    IOException ioEx = ExceptionUtils.create(IOException.class, "File not found");
    throw ioEx;
}
}

throwTarget

public static <T extends Throwable, TT extends Throwable> TT throwTarget(T source, Class<TT> thrownType)

Throws the given source exception wrapped into the specified target exception type.

If the source exception is already assignable to the target type, it will be rethrown directly. Otherwise, a new instance of the target type is created using suitable constructor arguments derived from the source exception.

Example Usages

`try {
    // some code that throws an IOException
` catch (IOException e) {
    throw ExceptionUtils.throwTarget(e, RuntimeException.class); // Rethrows as RuntimeException
}
}
`try {
    // some code that throws an IllegalArgumentException
` catch (IllegalArgumentException e) {
    throw ExceptionUtils.throwTarget(e, MyCustomException.class); // Wraps and throws as MyCustomException
}
}

This documentation was auto-generated from the source code of microsphere-java.

Home

java-annotations

java-core

jdk-tools

lang-model

annotation-processor

java-test

Clone this wiki locally