mitsuki31 / jmatrix

A lightweight Java library for basic introduction to matrix and linear algebra concepts.
https://mitsuki31.github.io/jmatrix/
Apache License 2.0
1 stars 0 forks source link

Refactor and Improve Exception Handling #115

Closed mitsuki31 closed 5 months ago

mitsuki31 commented 5 months ago

Overview

This pull request refactors and improves the exception handling functionality within the JMatrix library. It introduces new capabilities, updates Javadocs, and enhances the existing exception-raising mechanisms. These changes aim to provide more robust error handling, configurable auto-raise behavior, and comprehensive documentation to assist developers in effectively managing exceptions in their applications.

Changes Made

Description

This pull request addresses several key areas to improve the robustness and usability of exception handling in the JMatrix library. Here are the detailed descriptions of the improvements:

Enum for Error Codes

Introduced the JMErrorCode enum to standardize error codes within the JMatrix library. Each error code in the enum is associated with an integer, a string representation, and a descriptive message. This standardization simplifies error handling and enhances code readability.

Enum Constant Error Number Default Error Message
INVIDX 201 "Given index is out of bounds"
INVTYP 202 "Matrix has invalid type of matrix"
NULLMT 203 "Matrix is null"
UNKERR 400 "Unknown error"

Exception Improvements and Refactors

Auto-Raise Configuration

The auto-raise exceptions has been utilized by this library since the JMatrixUtils.raiseError method was introduced, this behavior makes any exceptions passed to the method will be printed and then exit the application immediately. This makes it harder for developers to handle the thrown exceptions.

These changes here introduced a new feature to configure the auto-raise exceptions behavior both from system property and environment variable. Users and developers can configure the behavior of auto-raise by set either the system property jm.autoraise or environment variable jm_autoraise to specific known values.

To deactivate the auto-raise behavior, set the value to one of these known values:

To activate the auto-raise behavior, set the value to one of these known values:

[!NOTE]
If both the system property jm.autoraise and environment variable jm_autoraise are set, the system property takes precedence. It is RECOMMENDED to configure using system property instead environment variable if possible.

The default configuration of auto-raise behavior is auto, which you might not have to configure and set the auto-raise configuration to auto or yes.

Example Usage

Using System Property

From command-line:

java -Djm.autoraise=manual -cp /path/to/jmatrix.jar Foo.java

During runtime execution (dynamic update):

// Set the auto-raise configuration to manual
System.setProperty("jm.autoraise", "manual");
Using Environment Variable

UNIX:

export jm_autoraise=manual
java -cp /path/to/jmatrix.jar Foo.java

Windows:

$env:jm_autoraise=manual
java -cp \path\to\jmatrix.jar Foo.java

[!WARNING]
You might encountering an ExceptionInInitializerError with IllegalArgumentException as cause exception during runtime execution, this exception was due to passing an unknown value to auto-raise configuration.

Deactivating the auto-raise configuration allows developers to handle exceptions using traditional try-catch blocks, providing more control over application flow and error handling.

In addition to this feature addition, within the internal context we also introduced new methods, getRaiseConfig and getRaiseConfigFromSystemProps, to safely retrieve the auto-raise configuration from environment variables and system properties, respectively. These methods handles SecurityException gracefully by logging a warning message instead of throwing an exception.

Serial Version UID Update

Updated the serialVersionUID values for all non-deprecated exceptions to a new format. The new format 430{dd}{MM}{YYYY}{EEE} includes the publication date and error code, where:

This format ensures that each serialVersionUID is unique and meaningful.

Documentation Improvements

Enhanced the Javadocs for several methods of exception classes to provide detailed descriptions, usage scenarios, cross-references, and detailed exception conditions. This ensures that developers have a comprehensive understanding of the method's functionality and usage.

Summary

This pull request significantly enhances the exception handling capabilities of the JMatrix library. It introduces configurable auto-raise behavior, updates Javadocs for clarity and completeness, and refactors existing exception mechanisms for robustness. These changes provide developers with more control over error handling, improve documentation, and ensure the library's reliability in managing exceptions.

By addressing these key areas, the pull request aims to make the JMatrix library more robust, flexible, and developer-friendly, supporting better error handling practices and improving overall code quality.