Online · Mandaluyong · Metro Manila

mBlock Coding and Interactive Robotics in Mandaluyong

mBlock coding and interactive robotics for Mandaluyong lets students write visual algorithms that control motor drives, line-following ideas and light indicators—on screen first, optional hardware later.

mBlock Mandaluyong

1. Introduction: Intentionally Generating Errors in Code

In earlier exception handling modules, the primary focus was on intercepting and handling errors generated automatically by Python at runtime. However, robust software design also requires the ability to intentionally generate errors ourselves.

  • Why generate errors intentionally? When code processes incoming data from users or external sources, Python's default runtime checks may not detect logical inconsistencies on their own.
  • Business logic & validation: If an application inspects data and encounters values that violate real-world rules or business logic—such as an impossible negative number for a person's age—it is dangerous to let the program proceed.
  • Instead of allowing faulty or illogical data to cause downstream bugs, the developer can explicitly trigger an exception using the raise keyword.

2. The raise Keyword and Error Anatomy

To manually trigger an exception, Python provides the raise keyword.

When using raise, you specify two key components:

  1. The exception type: Indicates the nature of the error (such as a ValueError, which signals an objection to the specific value of the data).
  2. The custom message: An explanatory text string detailing why the value is invalid or unacceptable.
raise ExceptionType("Descriptive error message")

Using this syntax alerts developers and surrounding code that an input check has failed and indicates where input validation or exception handling is needed.

3. Jupyter Notebook Example: Validating User Age

Step 1: Setting up the Input and Condition

Consider an application that accepts a person's age. If a user enters a negative value (such as -3), the code uses an if statement to verify that the value is non-negative:

# 1. Simulating an invalid user input
age = -3

# 2. Inspecting the value using an if statement
if age < 0:
    # 3. Explicitly raising a ValueError with a custom explanation
    raise ValueError("age cannot be negative")

Step 2: Execution and Output Analysis

When Python executes this cell in Jupyter Notebook, the condition age < 0 evaluates to True, triggering the raise statement:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In, line 6
      3 age = -3
      4
      5 if age < 0:
----> 6     raise ValueError("age cannot be negative")

ValueError: age cannot be negative
  • Targeted error type: Python stops execution and raises a ValueError, matching the exception type explicitly defined in the code.
  • Custom message display: Along with the error type, Python outputs the exact message supplied: "age cannot be negative".
  • Developer insight: This output clearly demonstrates what went wrong, signaling that checks must be applied to the inputs or that surrounding code must handle the exception properly.

4. Pairing raise with try...except for Controlled Handling

Raising an exception halts execution unless it is caught. In production applications, you can catch the custom-raised exception within a try...except block to log the error or prompt the user for valid data without terminating the program:

try:
    user_age = -3
    if user_age < 0:
        raise ValueError("age cannot be negative")
    print(f"User age recorded: {user_age}")

except ValueError as error:
    print(f"Validation Error caught: {error}")

Output:

Validation Error caught: age cannot be negative
  • The raise keyword triggers the ValueError when an invalid age is detected.
  • The except ValueError as error: block intercepts the error, extracts the custom text message, and prints a controlled notification instead of crashing the program.

Lesson Q&A

While Python automatically raises errors for technical failures (such as dividing by zero), it cannot anticipate custom business logic or domain rules on its own. If an application inspects data and finds an invalid or illogical value—such as a negative number entered for a user's age—the programmer can intentionally trigger an error using the raise keyword. This prevents corrupt or illogical data from propagating downstream and signals that an input check or exception handler is required.

The syntax for raising an exception is: raise ExceptionType("Custom explanatory message"). Using the raise keyword, the developer specifies the exception type (such as ValueError when objecting to a data value) along with a descriptive text message explaining the issue (such as "age cannot be negative"). When executed, Python generates that specific error and prints the custom message directly in the traceback.

Because -3 is less than 0, the if condition evaluates to True, triggering the raise statement. Python halts standard execution and outputs a ValueError displaying the exact message: "age cannot be negative". This indicates that an invalid value was detected and must either be corrected by the user or intercepted by an exception handler.

What children can learn

WhatsApp +92 322 421 7059

AI and Robotics Club | The 100 Mission AI & Robotics Hero Journey

Recommended self-paced course for parents & educators

mBlock Coding Fast Track for Beginners on Udemy.

Disclosure: This page contains affiliate links. We may earn from qualifying purchases at no extra cost to you. Links are intended for parents and educators, not as marketing directed at young children.

Visual Algorithms Meet Motion: mBlock Practice for Mandaluyong Learners

Mandaluyong families gain from visual robotics that stays optional on hardware. Enquire WhatsApp +92 322 4217059. No invented Mandaluyong studio.

Frequently asked questions

What makes mBlock effective for interactive robotics in Mandaluyong?

mBlock bridges Scratch block coding with physical mBot hardware and AI blocks. Students in Mandaluyong write visual code that executes real-time motor drives, line-following sensors, and LED indicators.

Can students in Mandaluyong participate online without owning a physical robot kit?

Yes! We utilize interactive software robotics simulators during live online classes, enabling students to write algorithms and simulate robot behavior on screen before purchasing physical hardware.

How can I request a free mBlock demo class for my child in Mandaluyong?

Click the WhatsApp button on this page to message our admissions team and arrange a free live trial session this week.

Explore related learning

QCArduino · Quezon City BGCRobotics · BGC CebuSTEM · Cebu