xxxxxxxxxx
{
# Try
} || {
# Executed when above fails
}
{
# Do stuff
} && {
# Only executed when above worked
}
xxxxxxxxxx
#!/bin/bash
# Try-Catch Block
(
# Statements to try
echo "Executing some commands..."
echo "Error may occur here: dividing by zero"
result=$(expr 10 / 0)
# If any error occurs, the script will jump to the catch block
)
# Catch Block
catch() {
local exitCode=$?
local errorMsg=$1
echo "An error occurred: $errorMsg"
echo "Exit code: $exitCode"
}
# Calling the catch function if an error occurred
catch "Error message here"