Error Handling

In this section, you’ll learn how to use the error handling.

BDScript 2

You must use BDScript 2 for the functions to work correctly.

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

What is this?

How $sum[] works? The explanation of how $endtry works will be given below.

Command code for !bot trigger:

$nomention
$sum[2;a]

Now let’s run each command:

!example !test 4 !bot ❌ Function $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?

Explanation

$try executes whole code (from top to bottom and from left to right) in its block and stops execution if it encounters an error.

Need to know

$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:
!example 7
  • Without $endtry:
!example $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
!example a ❌ Invalid number! !example 6 8

What is this?

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

NameDescriptionExample
commandReturns the name of the function that returned the error.$sum
messageReturns the error message that was received.expected integer in position 2, got ‘a’
sourceReturns the content of the line where the error occurred.$sum[2;a]
rowReturns the number of the row in the code where the error occurred.2
columnReturns the number of the column in the code where the error occurred.10

Syntax sensitivity

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
!example ❌ An error has occurred!
Function: $description
Error: expected valid value in position 2, got empty value

What is this?

How $description[] works?

$stop

Stops the command execution.

Syntax

$stop

Example

$nomention
$try
  $sum[2;$message]
$catch
  $stop
$endtry
Number: $message
!example a !example 5 7
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
!example 100 102 !example a 🚧 Description:
expected valid value in position 2, got empty value
Row: 2 | Column: 17 🔍 $sum[2;]

Create custom error for function

With error handling you can embed an error for…

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).

!example Hey, are you making an example for the guide? !example ⏱️ You have a cooldown! Wait 290 seconds.

What is this?

How $cooldown[] and $getCooldown[] works?

BDScript 2

Don’t forget to choose BDScript 2 when using the Error Handling functions!