Guards one or more statements against runtime errors and values thrown by the throw statement.
Try Statement
Try
{
Statements
}
The try statement is usually followed by a block - one or more statements (typically functions or expressions) enclosed in braces. If only a single statement is to be executed, it can be placed on the same line as try or on the next line, and the braces can be omitted. To specify code that executes only when try catches an error, use one or more catch statements.
If try is used without catch or finally, it is equivalent to having catch Error
with an empty block.
A value can be thrown by the throw statement or by the program when a runtime error occurs. When a value is thrown from within a try block or a function called by one, the following occurs:
The last catch can optionally be followed by else. If the try statement completes without an exception being thrown (and without control being transferred elsewhere by return, break or similar), the else statement is executed. Exceptions thrown by the else statement are not handled by its associated try statement, but may be handled by an enclosing try statement. Finally can also be present, but must be placed after else, and is always executed last.
The One True Brace (OTB) style may optionally be used with the try statement. For example:
try { ... } catch Error as err { ... } else { ... } finally { ... }
Throw, Catch, Else, Finally, Blocks, OnError
Demonstrates the basic concept of try/catch/throw.
try ; Attempts to execute code. { HelloWorld MakeToast } catch as e ; Handles the first error thrown by the block above. { MsgBox "An error was thrown!`nSpecifically: " e.Message Exit } HelloWorld() ; Always succeeds. { MsgBox "Hello, world!" } MakeToast() ; Always fails. { ; Jump immediately to the try block's error handler: throw Error(A_ThisFunc " is not implemented, sorry") }
Demonstrates basic error handling of built-in functions.
try { ; The following tries to back up certain types of files: FileCopy A_MyDocuments "\*.txt", "D:\Backup\Text documents" FileCopy A_MyDocuments "\*.doc", "D:\Backup\Text documents" FileCopy A_MyDocuments "\*.jpg", "D:\Backup\Photos" } catch { MsgBox "There was a problem while backing the files up!",, "IconX" ExitApp 1 } else { MsgBox "Backup successful." ExitApp 0 }
Demonstrates the use of try/catch dealing with COM errors. For details about the COM object used below, see Using the ScriptControl (Microsoft Docs).
try { obj := ComObject("ScriptControl") obj.ExecuteStatement('MsgBox "This is embedded VBScript"') ; This line produces an Error. obj.InvalidMethod() ; This line would produce a MethodError. } catch MemberError ; Covers MethodError and PropertyError. { MsgBox "We tried to invoke a member that doesn't exist." } catch as e { ; For more detail about the object that e contains, see Error Object. MsgBox("Exception thrown!`n`nwhat: " e.what "`nfile: " e.file . "`nline: " e.line "`nmessage: " e.message "`nextra: " e.extra,, 16) }
Demonstrates nesting try-catch statements.
try Example1 ; Any single statement can be on the same line with a Try statement. catch Number as e MsgBox "Example1() threw " e Example1() { try Example2 catch Number as e { if (e = 1) throw ; Rethrow the caught value to our caller. else MsgBox "Example2() threw " e } } Example2() { throw Random(1, 2) }