Class Err

All Implemented Interfaces:
Serializable

public final class Err extends Exception
Is the exception class with a reason.

This class has a field which indicates a reason for this exception. Typically the type of this field is Record. In this case, the class name of this record represents the reason, and the fields of the record hold the situation where the exception occurred.

Optionally, this exception class can notify its instance creation to pre-registered exception handlers. This notification feature can be enabled by specifying the system property -Dgithub.sttk.errs.notify=true when the JVM is started.

The example code of creating and throwing an exception is as follows:

public record FailToDoSomething(String name, int value) {
}

try {
    throw new Err(new FailToDoSomething("abc", 123));
} catch (Err e) {
    System.out.println(e.getMessage()); // => "FailToDoSomething { name=abc, value=123 }"
}
See Also:
  • Constructor Details

    • Err

      public Err(Object reason)
      Is the constructor which takes an object indicating the reason for this exception.
      Parameters:
      reason - A reason for this exception.
    • Err

      public Err(Object reason, Throwable cause)
      Is the constructor which takes an object indicating the reason and Throwable object indicating the cause for this exception.
      Parameters:
      reason - A reason for this exception.
      cause - A cause for this exception.
  • Method Details

    • getReason

      public Object getReason()
      Gets the reason for this exception. The type of the reason.
      Returns:
      The reason for this exception.
    • getMessage

      public String getMessage()
      Returns the message of this exception, that is the reason.
      Overrides:
      getMessage in class Throwable
      Returns:
      The message of this exception.
    • toString

      public String toString()
      Returns the detail message of this exception, that contains the reason, source file name, line number, and the cause if provided.
      Overrides:
      toString in class Throwable
      Returns:
      The message of this exception.
    • getFile

      public String getFile()
      Returns the name of the source file of this exception occurrence.

      This method can return null if this information is unavailable.

      Returns:
      The name of the source file of this error occurrence.
    • getLine

      public int getLine()
      Returns the line number of this exception occurrence in the source file.

      This method can return a negative number if this information is unavailable.

      Returns:
      The line number of this exception occurrence in the source file.
    • toRuntimeException

      public RuntimeException toRuntimeException()
      Creates a RuntimeException object for methods that cannot throw a Err.
      Returns:
      A RuntimeException object.
    • addSyncHandler

      public static void addSyncHandler(ErrHandler handler)
      Adds an ErrHandler object which is executed synchronously just after an Err is created. Handlers added with this method are executed in the order of addition and stop if one of the handlers throws a RuntimeException or an Error. NOTE: This feature is enabled via the system property: github.sttk.errs.notify=true
      Parameters:
      handler - An ErrHandler object.
    • addAsyncHandler

      public static void addAsyncHandler(ErrHandler handler)
      Adds an ErrHandler object which is executed asynchronously just after an Err is created. Handlers don't stop even if one of the handlers throw a RuntimeException or an Error. NOTE: This feature is enabled via the system property: github.sttk.errs.notify=true
      Parameters:
      handler - An ErrHandler object.
    • fixHandlers

      public static void fixHandlers()
      Prevents further addition of ErrHandler objects to synchronous and asynchronous exception handler lists. Before this is called, no Err is notified to the handlers. After this is called, no new handlers can be added, and Err(s) is notified to the handlers. NOTE: This feature is enabled via the system property: github.sttk.errs.notify=true