सामग्री पर जाएँ

लेसन 19 · Loops, Variables and Components

Loops, Switches, and Error Handling in KNIME

Repeat work with KNIME loop start and end nodes, branch with IF and CASE switches, and catch failures with try and catch nodes while keeping errors visible.

Some tasks repeat: build a report for every region, process each file separately, or try several model settings. Others need a choice: run step A on weekdays and step B at month end. KNIME handles these with flow control nodes.

Loops

A KNIME loop is a section of workflow between a loop start node and a loop end node. Everything between them runs again for each iteration, and the end node usually collects the results.

Loop start Repeats
Counting Loop Start A set number of times
Table Row To Variable Loop Start Once per row, with row values as variables
Group Loop Start Once per group of rows
Column List Loop Start Once per selected column
Chunk Loop Start Over blocks of rows

There are also generic, interval, and recursive loop starts. On the other side, Loop End collects rows from each iteration, Loop End (Column Append) collects results side by side, and Variable Condition Loop End stops when a condition is met.

Report per region

List One row per region
Loop Region becomes a variable
Filter Keep that region
Write One file each
1 / 2

A report-per-region loop looks like this:

TEXT
region list -> Table Row To Variable Loop Start
  -> Row Filter (region = variable) -> GroupBy -> Excel Writer (file name from variable)
  -> Variable Loop End
Python Stand-in: one output per region, with a safety check

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The empty region is reported, not silently skipped. Decide what your loop should do with empty input.

Before you use a loop

Loops can be slow and harder to debug. Many tasks do not need one: GroupBy already summarizes per group, and a reader can process a whole folder. Use a loop when each iteration must do something separate, such as writing different files or calling a service once per item. While building, test with two or three items. A Breakpoint node can pause a loop when a condition occurs.

Switches

A switch chooses which branch of a workflow runs.

  • The IF Switch offers two output branches, with only the active one executing. An End IF node merges the branches again.
  • The CASE Switch Data start and end nodes choose among several branches by position.

A flow variable usually decides which branch is active, for example a variable computed from the current date or a user's choice. Nodes on inactive branches are skipped rather than run on empty data.

Python Stand-in: choose a branch by variable

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The misspelled mode fails loudly, which is better than a default branch doing the wrong thing.

Try and catch

When a step can fail, such as a web request or a missing file, you can wrap it between a Try node (for data ports, there is "Try (Data Ports)") and a Catch Errors node. If the wrapped part fails, the catch node passes an alternative input forward and gives you error information as variables. There are variants for variables, database ports, and generic ports.

Use this to:

  • log the failure with the item that caused it,
  • continue processing other items,
  • send a notification or write an error file.

Do not use it to hide problems. A caught error should end up somewhere a person will see it. KNIME's best practices recommend wrapping critical nodes and reporting failures, so a problem does not go unnoticed. After a loop with error handling, count successes and failures, and fail the whole run if too many items failed.

A loop that writes or sends something, such as emails or database inserts, can repeat actions when rerun. Test with a safe target, add a way to detect already-processed items, and require approval before any step that contacts people or changes real records.

Debug a loop

A loop hides what happens inside each pass, so debug it with small steps. Limit the input to two or three items. Run the loop, then check the collected output. If it covers fewer items than expected, find which iteration produced nothing. Add a column that records the current item, so every collected row can be traced back to its iteration. Look for stepping options on the loop end node in your version, which let you inspect one pass at a time.

Collecting results safely

Different iterations can produce different columns, such as one region having an extra category. A loop end that expects identical structure may then fail or behave unexpectedly. Make the structure consistent inside the loop before collecting. For example, choose the columns explicitly after grouping.

Retries

When a loop calls an external service, some calls fail for temporary reasons. Retry only a limited number of times, wait between attempts, and stop if the failure looks permanent, such as an access denied message. Never blindly retry an action that may have succeeded unless repeating it is harmless. Record each attempt in a log table with the item, the time, and the result, so you can answer what happened without guessing.

Failure Retry?
Temporary network problem A few times, with a pause
Wrong password No, fix the credential
Invalid input No, report the row
Unknown result of a write Check first, then decide

Practice task

Create a table with three region names, including one that has no orders. Build a loop that writes one summary per region into a practice folder. Add a flag for the empty region instead of writing an empty file. Then rename one input column on purpose and run again. Observe where the failure appears, add error handling that records it, and confirm that the final log shows the failed item. Finally, run the workflow twice and check that the second run does not create duplicate or confusing output.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Which loop start runs once for each row and exposes its values as variables?

    It turns each row into flow variables for one iteration.
  2. 2. With an IF Switch, what happens to the inactive branch?

    Only the active branch runs.
  3. 3. What should happen to an error caught by Catch Errors?

    Catching an error is useful only if the problem is still visible.
  4. 4. When might a loop be unnecessary?

    GroupBy summarizes groups without a loop.

Summary

  • Loops run the nodes between a start and end node repeatedly.
  • Prefer simpler nodes when no loop is needed.
  • IF and CASE switches activate only the chosen branch.
  • Try and Catch Errors handle failures without hiding them.
  • Protect loops that send or change things.

Next: Components and Metanodes

आपकी प्रगति इसी ब्राउज़र में सेव होती है। खाते की ज़रूरत नहीं है।