Documentation
¶
Index ¶
Constants ¶
const RethrowPanic = "___throw_it___"
RethrowPanic is a special marker used to indicate that the original panic should be rethrown. This is used internally by the Throw function when called with nil.
Variables ¶
This section is empty.
Functions ¶
func Throw ¶
func Throw(e E)
Throw explicitly throws a panic that can be caught by a catch block. This is useful for rethrowing errors or creating custom errors.
Parameters:
- e: The error to be thrown. If nil, the original panic value will be rethrown.
Behavior:
- If e is nil, a special RethrowPanic marker is used to indicate the original error should be used.
- Otherwise, the provided value is directly used as the panic value.
Example:
Perform(func() {
if err := someOperation(); err != nil {
Throw(err) // Throw a specific error
}
}).Catch(func(e E) {
// Handle the error
})
Types ¶
type E ¶
type E any
E represents any type of error or panic value. This is an alias for any to allow catching any type of panic.
type EF ¶
type EF func(err E)
EF represents a function that takes an error parameter. Used for catch blocks to handle errors.
type F ¶
type F func()
F represents a function with no parameters and no return value. Used for try and finally blocks.
type It ¶
type It struct {
Error E // The error/panic value if one occurred
// contains filtered or unexported fields
}
It is the main structure that chains try, finally, and catch blocks. It holds the state of the error handling process.
func Perform ¶
Perform starts a try-catch-finally block by executing the provided function. This is the entry point for the error handling pattern.
Parameters:
- funcToTry: The main logic to execute within the try block.
Returns:
- *It: An instance of the It structure for chaining Finally and Catch calls.
Example:
Perform(func() {
// Code that might panic
}).Finally(func() {
// Cleanup code that always runs
}).Catch(func(e E) {
// Error handling code
})
func (*It) Catch ¶
Catch registers an error-handling function that is executed if an error occurs in the try block. If no error occurred, the catch block is skipped.
Parameters:
- funcCaught: The function to handle the error, which receives the error as a parameter.
Returns:
- *It: The same instance of It structures for chaining.
Behavior:
- If an error occurred in the try block, the catch function is executed with the error.
- If no error occurred, the catch function is skipped.
- The final function (if registered) is always executed, even if the catch block panics.
- If the catch block panics, the panic is propagated after the finally block executes.
- If Throw(nil) is called in the catch block, the original error is rethrown.
Example:
Perform(func() {
// Code that might panic
}).Catch(func(e E) {
// Handle the error
log.Printf("Error: %v", e)
})
func (*It) Finally ¶
Finally registers a function to be executed at the end of the try-catch block, regardless of whether an error occurred or not.
Parameters:
- finallyFunc: The function containing cleanup or finalization logic.
Returns:
- *It: The same instance of It structure for chaining.
Panics:
- If Finally is called more than once on the same It instance.
Example:
Perform(func() {
// Open a file
}).Finally(func() {
// Close the file, regardless of errors
}).Catch(func(e E) {
// Handle any errors
})