0% found this document useful (0 votes)
55 views3 pages

TCL Error Handling

Error Handling
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views3 pages

TCL Error Handling

Error Handling
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

9/23/2016

TclErrorHandling

TclErrorHandling
Advertisements

PreviousPage

NextPage

[Link]
eachofthesecommandsisshownbelow.

Errorsyntax
errormessageinfocode

Intheaboveerrorcommandsyntax,messageistheerrormessage,infoissetintheglobal
variableerrorInfoandcodeissetintheglobalvariableerrorCode.

CatchSyntax
catchscriptresultVarName

In the above catch command syntax, script is the code to be executed, resultVarNameis
variablethatholdstheerrorortheresult.Thecatchcommandreturns0ifthereisnoerror,
and1ifthereisanerror.
Anexampleforsimpleerrorhandlingisshownbelow

[Link]

1/3

9/23/2016

TclErrorHandling

#!/usr/bin/tclsh
procDiv{ab}{
if{$b==0}{
error"Errorgeneratedbyerror""InfoStringforerror"401
}else{
return[expr$a/$b]
}
}
if{[catch{puts"Result=[Div100]"}errmsg]}{
puts"ErrorMsg:$errmsg"
puts"ErrorCode:$errorCode"
puts"ErrorInfo:\n$errorInfo\n"
}
if{[catch{puts"Result=[Div102]"}errmsg]}{
puts"ErrorMsg:$errmsg"
puts"ErrorCode:$errorCode"
puts"ErrorInfo:\n$errorInfo\n"
}

Whentheabovecodeisexecuted,itproducesthefollowingresult
ErrorMsg:Errorgeneratedbyerror
ErrorCode:401
ErrorInfo:
InfoStringforerror
(procedure"Div"line1)
invokedfromwithin
"Div100"
Result=5

As you can see in the above example, we can create our own custom error messages.
Similarly,[Link]
#!/usr/bin/tclsh
catch{setfile[[Link]]}result
puts"ErrorMsg:$result"
puts"ErrorCode:$errorCode"
puts"ErrorInfo:\n$errorInfo\n"

Whentheabovecodeisexecuted,itproducesthefollowingresult
ErrorMsg:couldn'topen"[Link]":nosuchfileordirectory
ErrorCode:POSIXENOENT{nosuchfileordirectory}
ErrorInfo:
couldn'topen"[Link]":nosuchfileordirectory
whileexecuting
"[Link]"
[Link]

2/3

9/23/2016

TclErrorHandling

PreviousPage

NextPage
Advertisements

Write for us

FAQ's

Helping

Contact

Copyright 2016. All Rights Reserved.


Enter email for newsletter

[Link]

go

3/3

You might also like