Error Handling in Rui

Rui provides a straightforward way to handle common mistakes and errors in your code. Below are some examples of errors and how you might encounter them.

Variable Name Errors

Rui has specific rules for variable names. These errors occur when you violate naming conventions:

// These will cause clear error messages:

// suppose 123name = "invalid"     // Error: variable names cannot start with a number
// suppose first-name = "invalid"  // Error: variable names cannot contain hyphens
// suppose if = "invalid"         // Error: 'if' is a reserved word

// Valid variable names
suppose validName = "Hello"
suppose name123 = "World"
suppose _private = "Private"
suppose snake_case = "Snake"

Type Errors

Type errors occur when you try to perform operations on incompatible data types:

suppose str = "hello"
suppose num = 42

// These will cause runtime errors:
// write(str + num)  // Works due to automatic conversion
// write(str - num)  // Error: cannot subtract number from string

// Valid operations
write(str + " " + num)  // "hello 42"
write(num + 10)         // 52

Syntax Errors

Syntax errors occur when the code does not follow the correct structure of the language.

// Missing closing brace
  define greet(name) {
      write("Hello, " + name + "!")
  }  // Missing closing brace
  
  // Missing operator
  suppose result = 5 3  // Missing operator between 5 and 3
  
  // Unmatched quotes
  suppose text = "Hello World  // Missing closing quote
  

Runtime Errors

These errors occur while the program is running.

// Division by zero
  suppose result = 10 / 0  // Error: Division by zero
  
  // Accessing undefined variable
  write(undefinedVar)  // Error: Variable not defined
  
  // Calling a non-function
  suppose notAFunction = 42
  notAFunction()  // Error: Trying to call a number
  

Logical Errors

Logical errors are mistakes in the program's logic, often harder to detect.

// Incorrect loop condition
  suppose i = 0
  until (i < 5) {
      write("Count: " + i)
      // Missing: i = i + 1  // This creates an infinite loop
  }
  
  // Wrong comparison
  suppose age = 18
  if (age = 18) {
      write("You are 18")  // Should use == not =
  }
  
  // Off-by-one error
  suppose numbers = [1, 2, 3, 4, 5]
  suppose i = 0
  until (i <= length(numbers)) {
      write(numbers[i])  // Will try to access index 5 (out of bounds)
      i = i + 1
  }
  

Type Errors

Type errors occur when an operation is performed on incompatible types.

// Adding number to string
  suppose result = "Hello" + 5  // Valid in Rui (concatenation), but might not be intended
  
  // Using string as number
  suppose num = "abc" * 2  // Error: Cannot multiply string with number
  
  // Accessing property of non-object
  suppose x = 42
  write(x.name)  // Error: Number has no property 'name'
  

Best Practices

  • Always check variable initialization.
  • Use meaningful variable and function names.
  • Test your code frequently.
  • Avoid infinite loops by ensuring correct loop conditions.

What's Next?

Learn how to debug Rui code efficiently using built-in tools and error messages.

Version Information

Current Version: 1.0.9
Author: Pratik Deshpande

Next Steps