MACSCRPT Archives

June 2009

MACSCRPT@LISTSERV.DARTMOUTH.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
Anthony Adachi <[log in to unmask]>
Reply To:
Macintosh Scripting Systems <[log in to unmask]>
Date:
Mon, 1 Jun 2009 14:57:31 -0400
Content-Type:
text/plain
Parts/Attachments:
text/plain (76 lines)
Hello,

In AppleScript and many other programming languages one can throw
deliberate errors of a specific kind as well as catch-trap for specific
errors. I'm having a hard time determining how to do this in bash 3.2
with it's 'trap' command. 

For instance, a very simple example in AppleScript...

try
	error 99
on error 99
	say "Caught expected error."
end try

say "doing more tests..."


Moreover, I can't figure out whether or not if it's even possible to
catch for specific thrown errors defined by a scripter in bash. My own
tests have led to a dead end and I haven't come any better understanding
from reading the bash info docs, and numerous online and in print
examples of the 'trap' command. 


Basically, I wish to...

1. First of all, have scripts throw a specifically, identifiable, unique
error when a certain known bad state has been encountered that they
can't handle. 

2. Secondly, to able to test for whether scripts are throwing an
specific error under a certain condition.


3. Trap for and catch only that specific error and then keep on going,
performing further automated tests.

Is this possible in bash?


Below is an example which might better illustrate what I've attempted to
do. It unfortunately, doesn't do what's needed at the moment. i.e.- It
will catch all errors and exits but not a specific one.

#--- The example below traps all errors but not the specific one.

particular_function()
{
  current_status="some known bad condition"
  if [ "$current_status" = "some known bad condition" ]; then
    # Encountered some known condition we can't handle so throw specific
error.
    echo "Encountered a, we knew this would happen, type error."
    exit -99
  fi
}

test_method_for_expected_error()
{
  trap 'echo "function threw expected error code as it should."' EXIT
  particular_function
}

test_method_for_expected_error

echo "Continued passed caught error as desired and continuing further
tests..."

#--- end of example


Thanks,

-Anthony 

ATOM RSS1 RSS2