mlowery Posted August 23, 2009 Share Posted August 23, 2009 I write a lot of scripts for my use only, and so I frequently don't take the time to add in proper error-handling. If a script fails, I'll just dig in and debug it. Recently, I decided to work up a very simple error handler function that would allow me to do basic error handling on a single line, making it easy to add to scripts as I write them. Thought it would be worth sharing for others or feedback on improvements. ;=============================================================================== ; Description: Display an error message and optionally exit or set ; error codes and return values. Enables single-line ; error handling for basic needs. ; Parameter(s): $txt = message to display ; [$exit] = 1 to exit after error thrown, 0 to return ; [$ret] = return value ; [$err] = error code to return to parent function if $exit = 0 ; [$ext] = extended error code to return to parent function if $exit = 0 ; [$time] = time to auto-close message box, in seconds (0 = never) ; Requirement(s): None ; Return Value(s): ; Note(s): Icon is STOP for EXIT/FATAL errors and EXCLAMATION for NO_EXIT/WARNING errors. ; For single-line error-reporting. If reporting an error in a function, ; can call this with a Returned value as: ; If $fail Then Return _ThrowError("failed",0,$return_value) ;=============================================================================== Func _ThrowError($txt, $exit = 0, $ret = "", $err = 0, $ext = 0, $time = 0) If $exit = 0 Then MsgBox(48, @ScriptName, $txt, $time) ; Exclamation, return with error code Return SetError($err, $ext, $ret) Else MsgBox(16, @ScriptName, $txt, $time) ; Stop, quit after error Exit ($err) EndIf EndFunc Examples in use: $file = FileOpen($filename,0) if $file =-1 then _ThrowError("File " & $file & " not found!",1) ; Exit when msgbox closed $clip = ClipGet() if $clip = "" then _ThrowError("Clipboard is empty. Continuing...", 0, 0, 0, 3) ; No exit, no error, auto-close in 3 seconds Func parse_html_title($source_string) Local $title = StringRegExp($source_string,"<title>([^>]*)</title>",1) If Not IsArray($title) then Return _ThrowError("Failed to extract title", 0, "-no title found-", 1, 0, 3) ; No exit, error=1, auto-close in 3 seconds Return $title[0] EndFunc abdulrahmanok 1 Link to comment Share on other sites More sharing options...
reb Posted August 23, 2009 Share Posted August 23, 2009 I write a lot of scripts for my use only, and so I frequently don't take the time to add in proper error-handling. If a script fails, I'll just dig in and debug it. Recently, I decided to work up a very simple error handler function that would allow me to do basic error handling on a single line, making it easy to add to scripts as I write them. Thought it would be worth sharing for others or feedback on improvements. ;=============================================================================== ; Description: Display an error message and optionally exit or set ; error codes and return values. Enables single-line ; error handling for basic needs. ; Parameter(s): $txt = message to display ; [$exit] = 1 to exit after error thrown, 0 to return ; [$ret] = return value ; [$err] = error code to return to parent function if $exit = 0 ; [$ext] = extended error code to return to parent function if $exit = 0 ; [$time] = time to auto-close message box, in seconds (0 = never) ; Requirement(s): None ; Return Value(s): ; Note(s): Icon is STOP for EXIT/FATAL errors and EXCLAMATION for NO_EXIT/WARNING errors. ; For single-line error-reporting. If reporting an error in a function, ; can call this with a Returned value as: ; If $fail Then Return _ThrowError("failed",0,$return_value) ;=============================================================================== Func _ThrowError($txt, $exit = 0, $ret = "", $err = 0, $ext = 0, $time = 0) If $exit = 0 Then MsgBox(48, @ScriptName, $txt, $time) ; Exclamation, return with error code Return SetError($err, $ext, $ret) Else MsgBox(16, @ScriptName, $txt, $time) ; Stop, quit after error Exit ($err) EndIf EndFunc Examples in use: $file = FileOpen($filename,0) if $file =-1 then _ThrowError("File " & $file & " not found!",1) ; Exit when msgbox closed $clip = ClipGet() if $clip = "" then _ThrowError("Clipboard is empty. Continuing...", 0, 0, 0, 3) ; No exit, no error, auto-close in 3 seconds Func parse_html_title($source_string) Local $title = StringRegExp($source_string,"<title>([^>]*)</title>",1) If Not IsArray($title) then Return _ThrowError("Failed to extract title", 0, "-no title found-", 1, 0, 3) ; No exit, error=1, auto-close in 3 seconds Return $title[0] EndFunc Your code works well. I will find this to be useful when scripting. Thank you for sharing REB MEASURE TWICE - CUT ONCE Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now