Class Exc

All Implemented Interfaces:
Serializable

public final class Exc 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 excepton is as follows:


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

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

    • Exc

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

      public Exc(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 occurrance.

      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 occurrance in the source file.

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

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

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

      public static void addSyncHandler(ExcHandler handler)
      Adds an ExcHandler object which is executed synchronously just after an Exc 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 ExcHandler object.
    • addAsyncHandler

      public static void addAsyncHandler(ExcHandler handler)
      Adds an ExcHandler object which is executed asynchronously just after an Exc 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 ExcHandler object.
    • fixHandlers

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