Class Err
java.lang.Object
java.lang.Throwable
java.lang.Exception
com.github.sttk.errs.Err
- All Implemented Interfaces:
Serializable
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddAsyncHandler(ErrHandler handler) Adds anErrHandlerobject which is executed asynchronously just after anErris created.static voidaddSyncHandler(ErrHandler handler) Adds anErrHandlerobject which is executed synchronously just after anErris created.static voidPrevents further addition ofErrHandlerobjects to synchronous and asynchronous exception handler lists.getFile()Returns the name of the source file of this exception occurrence.intgetLine()Returns the line number of this exception occurrence in the source file.Returns the message of this exception, that is the reason.Gets the reason for this exception.Creates aRuntimeExceptionobject for methods that cannot throw aErr.toString()Returns the detail message of this exception, that contains the reason, source file name, line number, and the cause if provided.Methods inherited from class Throwable
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace
-
Constructor Details
-
Err
Is the constructor which takes an object indicating the reason for this exception.- Parameters:
reason- A reason for this exception.
-
Err
-
-
Method Details
-
getReason
Gets the reason for this exception. The type of the reason.- Returns:
- The reason for this exception.
-
getMessage
Returns the message of this exception, that is the reason.- Overrides:
getMessagein classThrowable- Returns:
- The message of this exception.
-
toString
-
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
Creates aRuntimeExceptionobject for methods that cannot throw aErr.- Returns:
- A
RuntimeExceptionobject.
-
addSyncHandler
Adds anErrHandlerobject which is executed synchronously just after anErris created. Handlers added with this method are executed in the order of addition and stop if one of the handlers throws aRuntimeExceptionor anError. NOTE: This feature is enabled via the system property:github.sttk.errs.notify=true- Parameters:
handler- AnErrHandlerobject.
-
addAsyncHandler
Adds anErrHandlerobject which is executed asynchronously just after anErris created. Handlers don't stop even if one of the handlers throw aRuntimeExceptionor anError. NOTE: This feature is enabled via the system property:github.sttk.errs.notify=true- Parameters:
handler- AnErrHandlerobject.
-
fixHandlers
public static void fixHandlers()Prevents further addition ofErrHandlerobjects to synchronous and asynchronous exception handler lists. Before this is called, noErris notified to the handlers. After this is called, no new handlers can be added, andErr(s) is notified to the handlers. NOTE: This feature is enabled via the system property:github.sttk.errs.notify=true
-