Tcl - Hata İşleme
Tcl'de hata yönetimi, error ve catchkomutlar. Bu komutların her birinin sözdizimi aşağıda gösterilmiştir.
Hata sözdizimi
error message info codeYukarıdaki hata komutu sözdiziminde, mesaj, hata mesajıdır, bilgi, errorInfo global değişkeninde ayarlanır ve kod, genel değişken errorCode'da ayarlanır.
Sözdizimini Yakala
catch script resultVarNameYukarıdaki catch komutu sözdiziminde, komut dosyası çalıştırılacak koddur, resultVarName ise hatayı veya sonucu tutan değişkendir. Catch komutu, hata yoksa 0, hata varsa 1 döndürür.
Basit hata işlemeye bir örnek aşağıda gösterilmiştir -
#!/usr/bin/tclsh
proc Div {a b} {
   if {$b == 0} {
      error "Error generated by error" "Info String for error" 401
   } else {
      return [expr $a/$b]
   }
}
if {[catch {puts "Result = [Div 10 0]"} errmsg]} {
   puts "ErrorMsg: $errmsg"
   puts "ErrorCode: $errorCode"
   puts "ErrorInfo:\n$errorInfo\n"
}
if {[catch {puts "Result = [Div 10 2]"} errmsg]} {
   puts "ErrorMsg: $errmsg"
   puts "ErrorCode: $errorCode"
   puts "ErrorInfo:\n$errorInfo\n"
}Yukarıdaki kod çalıştırıldığında, aşağıdaki sonucu verir -
ErrorMsg: Error generated by error
ErrorCode: 401
ErrorInfo:
Info String for error
   (procedure "Div" line 1)
   invoked from within
"Div 10 0"
Result = 5Yukarıdaki örnekte görebileceğiniz gibi, kendi özel hata mesajlarımızı oluşturabiliriz. Benzer şekilde, Tcl tarafından üretilen hatayı yakalamak da mümkündür. Aşağıda bir örnek gösterilmiştir -
#!/usr/bin/tclsh
catch {set file [open myNonexistingfile.txt]} result
puts "ErrorMsg: $result"
puts "ErrorCode: $errorCode"
puts "ErrorInfo:\n$errorInfo\n"Yukarıdaki kod çalıştırıldığında, aşağıdaki sonucu verir -
ErrorMsg: couldn't open "myNonexistingfile.txt": no such file or directory
ErrorCode: POSIX ENOENT {no such file or directory}
ErrorInfo:
couldn't open "myNonexistingfile.txt": no such file or directory
   while executing
"open myNonexistingfile.txt"