Table of Contents

Getting started with Tcl: example 11

This page is part of the Airplug documentation related to Tcl/Tk.
Back to the Tcl/Tk documentation page / Back to the Airplug documentation page

Dealing with errors

Script

#!/usr/bin/tclsh
# errors

# Catching errors
if [ catch { puts stdout "MyNumber = $MyNumber" } MyErrorMsg ] {
  puts stderr "An error occured. MyErrorMsg = $MyErrorMsg"
} {
  puts stdout "No error occured. MyErrorMsg = $MyErrorMsg"
}
 
set MyNumber 5
 
if [ catch { puts stdout "MyNumber = $MyNumber" } MyErrorMsg ] {
  puts stderr "An error occured. MyErrorMsg = $MyErrorMsg"
} {
  puts stdout "No error occured. MyErrorMsg = $MyErrorMsg"
}
 
puts stdout "----"

# Catching errors in procedures
proc MarchePas { } {
    puts stdout "  In procedure MarchePas; error"
    error "error in the procedure"
}
 
proc Marche { } {
    puts stdout "  In procedure Marche; all is right"
}
 
if [ catch MarchePas errorMsg ]  {
    puts stderr "An error occured when calling the MarchePas procedure: \"$MyErrorMsg\""
    # reset the error
    set errorInfo
} { 
    puts stdout "No error occured when calling the MarchePas procedure. MyErrorMsg = $MyErrorMsg"
}
 
 
if [ catch Marche MyErrorMsg ]  {
    puts stderr "An error occured when calling the Marche procedure: \"$MyErrorMsg\""
    # reset the error
    set errorInfo
} { 
    puts stdout "No error occured when calling the Marche procedure. MyErrorMsg = $MyErrorMsg"
}

Output

An error occured. MyErrorMsg = can't read "MyNumber": no such variable
MyNumber = 5
No error occured. MyErrorMsg = 
----
  In procedure MarchePas; error
An error occured when calling the MarchePas procedure: "error in the procedure"
  In procedure Marche; all is right
No error occured when calling the Marche procedure. MyErrorMsg = 

<br/>


This page is part of the Airplug documentation related to Tcl/Tk.
Back to the Tcl/Tk documentation page / Back to the Airplug documentation page