If it's an MS-DOS type of button then you can use the usual DOS batch commands for that, but keep in mind that strange things will happen if you mix in any Opus commands. (Behind the scenes, Opus splits the batch up into smaller batch files every time it has to run an Opus command, so conditional logic and loops will be broken.)
For non-DOS scripts there isn't any way to handle errors. Opus commands don't have any branching or conditional logic. (That's not quite true. You can make them do different things when Ctrl/Alt/Shift are held down but that's not relevant here.)
Essentially, if you want to do anything complex with conditional logic you are often better off writing a VBScript which you run from Opus, using Opus to pass in arguments like lists of selected files, source and destination directories, and so on.
(If you need to pass a lot of selected files then you can avoid command-line length limits by using the filem modified. For example, {allfilepath|filem} will write all the selected file paths into a temporary file and then pass the name of the temporary file to the program/script.)
It works fine here. This button will echo the source path, not "{sourcepath}":
<?xml version="1.0"?>
<button backcol="none" display="both" textcol="none">
<label>Test</label>
<icon1>#selectwild</icon1>
<function type="batch">
<instruction>@set test = {sourcepath}</instruction>
<instruction>echo {$test}</instruction>
<instruction>pause</instruction>
</function>
</button>
I think (not completely sure) that the @script modifier is only useful with Rename buttons at the moment.
To pass variables from Opus to a script just use the command line. Here's an example script written to be called from Opus: