Error Handling
In this section, you’ll learn how to use the error handling.
Content
Functions Used > $try > $endtry > $catch > $error[] > $stop > Error Type > Create BDScript custom error > Create custom error for function
Functions Used
Support Functions Used
$try
Opens the Error Handling block.
Syntax
$try
Example
- Let’s create 3 commands with different triggers:
!example
,!test
and!bot
.
Command code for !example
trigger:
$nomention
$try
$sum[2;a]
$endtry
Command code for !test
trigger:
$nomention
$try
$sum[2;2]
$endtry
Command code for !bot
trigger:
$nomention
$sum[2;a]
Now let’s run each command:
$sum
at 2:9
returned an error: expected integer in position 2, got 'a'
As you can see only !test
command returned a response, but why?
$try
executes whole code (from top to bottom and from left to right) in its block and stops execution if it encounters an error.
$try
block does not support some errors. For example not closed bracket in the function.
$endtry
Closes the Error Handling block.
Syntax
$endtry
Example
$nomention
$try
$sum[2;5]
$endtry
- With
$endtry
:
- Without
$endtry
:
$try
not closed with $endtry
or invalid use of $sum
$catch
Used to create a sub-block between $try
and $endtry
that will contain the code that will be executed when an error occurs.
Syntax
$catch
Example
$nomention
$try
$sum[2;$message]
$catch
❌ Invalid number!
$endtry
How $message
works?
$error
Used in the $catch
block to return the BDScript 2 error information.
Syntax
$error[Type]
Parameters
Type
(Type: Enum || Flag: Required)
: What type of error data to return.
Error Type
Name | Description | Example |
---|---|---|
command | Returns the name of the function that returned the error. | $sum |
message | Returns the error message that was received. | expected integer in position 2, got ‘a’ |
source | Returns the content of the line where the error occurred. | $sum[2;a] |
row | Returns the number of the row in the code where the error occurred. | 2 |
column | Returns the number of the column in the code where the error occurred. | 10 |
The type input must only be written in lowercase letters.
❌ Not correct:
$error[ROW]
✅ Correct:
$error[row]
Example
$nomention
$try
$description[Hello developer!;]
$catch
❌ An error has occurred!
Function: $error[command]
Error: $error[message]
$endtry
Function: $description
Error: expected valid value in position 2, got empty value
How $description[]
works?
$stop
Stops the command execution.
Syntax
$stop
Example
$nomention
$try
$sum[2;$message]
$catch
$stop
$endtry
Number: $message
Number: 5
As we can see, the bot did not send “Number: a” when we tried to use “!example a
”.
Create BDScript custom error
With $error[]
you can create custom error for code!
$nomention
$try
$sum[2;$message]
$catch
$title[Error!]
$description[🚧 Description:
```$error[message]```]
$addField[Location;Row: `$error[row]` | Column: `$error[column]`]
$footer[🔍 $error[source]]
$endtry
expected valid value in position 2, got empty value
2
| Column: 17
Create custom error for function
With error handling you can embed an error for…
- Cooldown functions:
$serverCooldown[]
,$globalCooldown[]
,$cooldown[]
; - “Only If” Functions:
$onlyAdmin[]
,$onlyIf[]
and other similar “Only If” functions…; - Enabled function:
$enabled[]
.
Let’s create embed error for $coodlwon[]
function:
$nomention
$try
$cooldown[5m;]
Hey, are you making an example for the guide?
$catch
$description[⏱️ You have a cooldown! Wait $getCooldown[normal] seconds.]
$endtry
This example will work with other functions too, just replace $cooldown[]
function (Specified above).
How $cooldown[]
and $getCooldown[]
works?
Don’t forget to choose BDScript 2 when using the Error Handling functions!