Re: Error Context (used to be Inheritance)
Re: Error Context (used to be Inheritance)
- Subject: Re: Error Context (used to be Inheritance)
- From: Axel Luttgens <email@hidden>
- Date: Fri, 23 Jan 2004 15:24:35 +0100
Wallace, William wrote:
[...]
This is really the heart of the issue I was trying to sort out. I was
concerned that I was setting myself up for and infinite error loop or
at least a difficult to debug string of nested errors. As it turns out
the point is moot for the moment.
For example: [CORRECTED VERSION FROM WILLIAM'S SECOND POST]
--main script
global scriptVar1, scriptVar2, errNum, errMsg
set scriptVar1 to (load script alias "path:to:script1")
set scriptVar2 to (load script alias "path:to:script2")
try
set p to 1/0
on error errMsg number errNum
tell scriptVar1 to doSomething(errNum, errMsg)
display dialog "Main Error! " & errNum & return & errMsg
end try
--loaded script 1
on doSomething(myNum, myMsg)
try
set myBungle to myNum / myMsg
on error errMsg number errNum
tell my parent's scriptVar2 to doSomethingElse(errNum, errMsg)
display dialog "Script 1 Error! " & errNum & return & errMsg
end try
end doSomething
--loaded script 2
on doSomethingElse(theNum, theMsg)
try
set myFauxPas to (theMsg + theNum)
on error errMsg number errNUm
display dialog "Script 2 Error! " & errNum & return & errMsg
end try
end doSomethingElse
What I would have hoped would happen here is that I would get three
dialog boxes in a row, each with their appropriate error number and
error message. What I was afraid I would get was three dialog boxes
each containing the "Script 2 Error". What I actually get is
"Exectution Error! scriptVar2 doesn't understand the doSomethingElse
message". So I still don't have a firm grasp of this concept it seems.
Can you show me where I've gone wrong in the above?
I guess you now know why ;-)
So, assuming the main script will be run as an application, some
enhancements are still possible (for a clarification of the
inheritance/scoping matters, I mean); so, if you allow, I'll procede as
in my previous post (the one with a subject starting "Re: Error Number...").
A proposal for a slightly adapted version of your code:
-- Main script
set scriptVar1 to load script alias "path:to:script1"
set scriptVar2 to load script alias "path:to:script1"
try
set p to 1 / 0
on error errMsg number errNum
tell scriptVar1 to doSomething(errNum, errMsg)
display dialog "Main Error! " & errNum & return & errMsg
end try
-- loaded script 1
on doSomething(myNum, myMsg)
global scriptVar2
try
set myBungle to myNum / myMsg
on error errMsg number errNum
tell scriptVar2 to doSomethingElse(errNum, errMsg)
display dialog "Script 1 Error! " & errNum & return & errMsg
end try
end doSomething
-- loaded script 2
on doSomethingElse(theNum, theMsg)
try
set myFauxPas to (theMsg + theNum)
on error errMsg number errNum
display dialog "Script 2 Error! " & errNum & return & errMsg
end try
end doSomethingElse
Some comments about the suggested changes:
1. errNum and errMsg have been deleted from main script's "global ..."
statement.
This for two reasons.
First, an "on error ..." does the job for defining variables bearing the
names you want them to have ("errMsg" and "errNum" in this case); there
is thus no need to define such variables by other means than just
putting their names into the "on error ..." statement.
Second, you never use them as global variables, because you either use
them directly where their values are immediately accessible (as in main
script's "display dialog ..." statement) or through arguments passed
upon handler calls (as in main script's "tell scriptVar1 to ..." statement).
2. Moreover, the whole "global ..." statement has been removed.
For the same reasons than those exposed in my previous post.
3. A "global scriptVar2" statment has been introduced into script1.
4. The "my parent's scriptVar2" has been replaced by just "scriptVar2"
For the same reasons than those exposed in my previous post.
Note that script1 refers one global ("scriptVar2") and 5 locals:
- "myNum", "myMsg": declared through handler's arguments
- "myBungle": implicitely declared as a local to the handler (the first
occurrence of that identifier in script1's code is in a "set ..."
statement within a handler).
- "errMsg", "errNum": declared through a "on error ..." statement.
and thus doesnt' make use of any property at all!
Similar remarks may be emitted about script2.
HTH,
Axel
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.