Class Exc
java.lang.Object
java.lang.Throwable
java.lang.Exception
com.github.sttk.errs.Exc
- 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 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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
addAsyncHandler
(ExcHandler handler) Adds anExcHandler
object which is executed asynchronously just after anExc
is created.static void
addSyncHandler
(ExcHandler handler) Adds anExcHandler
object which is executed synchronously just after anExc
is created.static void
Prevents further addition ofExcHandler
objects to synchronous and asynchronous exception handler lists.getFile()
Returns the name of the source file of this exception occurrance.int
getLine()
Returns the line number of this exception occurrance in the source file.Returns the message of this exception, that is the reason.Gets the reason for this exception.Creates aRuntimeException
object for methods that cannot throw aExc
.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 java.lang.Throwable
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace
-
Constructor Details
-
Exc
Is the constructor which takes an object indicating the reason for this exception.- Parameters:
reason
- A reason for this exception.
-
Exc
-
-
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:
getMessage
in classThrowable
- Returns:
- The message of this exception.
-
toString
-
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
Creates aRuntimeException
object for methods that cannot throw aExc
.- Returns:
- A
RuntimeException
object.
-
addSyncHandler
Adds anExcHandler
object which is executed synchronously just after anExc
is created. Handlers added with this method are executed in the order of addition and stop if one of the handlers throws aRuntimeException
or anError
. NOTE: This feature is enabled via the system property:github.sttk.errs.notify=true
- Parameters:
handler
- AnExcHandler
object.
-
addAsyncHandler
Adds anExcHandler
object which is executed asynchronously just after anExc
is created. Handlers don't stop even if one of the handlers throw aRuntimeException
or anError
. NOTE: This feature is enabled via the system property:github.sttk.errs.notify=true
- Parameters:
handler
- AnExcHandler
object.
-
fixHandlers
public static void fixHandlers()Prevents further addition ofExcHandler
objects to synchronous and asynchronous exception handler lists. Before this is called, noExc
is notified to the handlers. After this is called, no new handlers can be added, andExc
(s) is notified to the handlers. NOTE: This feature is enabled via the system property:github.sttk.errs.notify=true
-