Is it possible to get the line number, where the script threw an error?
Example:
try
set a to "abc" + "123"
on error line number num
display dialog "Error on line number " & num
end try
i don't think so try statements look like this
try
set a to "abc" + "123"
on error errMsg
display dialog "ERROR: " & errMsg
end try
but you could look at script debugger which will show you what line your error occurred on
another alternative is to get textmate which goes for $52 when it errors it gives you the line number and is also useful for writing code in many languages
Actually the on error syntax include the error number also (but no line number):
try
set a to "abc" + "123"
on error errorMessage number errorNumber
log ("errorMessage: " & errorMessage & ", errorNumber: " & errorNumber)
end try
You can use semaphores to mark your progress:
try
... your code here ...
set lineNumber to "17"
... more code here
set lineNumber to "18"
... more code here
on error errorMessage number errorNumber
log ("(line #" & lineNumber & ") errorMessage: " & errorMessage & ", errorNumber: " & errorNumber)
end try
And I'll 2nd mcgrailm's recommendation for Script Debugger!
Satimage's Smile is of great help when it comes to debugging an applescript.
And it's free.
Plus it's French (hehe).
Definitely a great tool !
Late to the party here, but in respect to Script Debugger, here is a perhaps useful response from Mark Alldritt:
Yes, turn on Break On Exceptions. This cause the debugger to break at the point where an exception is thrown. The debugger also shows the sate of all known variable at the time the exception is thrown. You can then step into the 'on error' block.
Cheers
-Mark
On 2013-01-24, at 8:43 AM, Dan wrote:
When a script throws an error in a Try block, is there any reasonable way to display the line where the error occurred?
Related
Hello I am currently using IBM Spss, and I was wondering if there is a way when the code throws an exception to be able to obtain the line of code where the exception occurred? I am able to obtain the line number is there a way to access the stack of the lines that were executed and pull this information from there? Any ideas would be great Thank you in advance!
For Example:
Dim x, y, z
On Error Goto ErrorHandler
x = 30
y = 0
z = x / y 'would like to grab this code since this is where the error occurred
Exit
ErrorHandler:
debug.Log(Err.Description + _
" error occurred on line number " + _
CText(Err.LineNumber))
No, there is no way to map the line number to the actual code unless you keep your own index, allowing for macro expansion etc.
What you can do, though, is to submit the code in blocks and catch the exception for that block, which narrows down the location. In the limit, if each line is its own block, you will know exactly where the error occurred.
So I'm mounting different smb shares, and want to display a relevant text according to the error number, and do nothing for the numbers I have not defined.
Sort of what i'm looking for:
on error
if error_number - 5014 then
display dialog "Can't connect to" & chosen as text
else if error_number - 5016 then
display dialog "Can't connect to" & chosen as text
else if error_number then --do nothing
end if
Right now I get this popup from MacOSX :
"There was a problem connecting to the server ....."
and then then my defined error message. How do I get rid of the double popup? I prefer just getting my notification, not the one from the OS.
This is pretty easy because error provides an optional parameter number
on error errorMessage number errorNumber
if errorNumber = -36 then
display dialog "Can't connect to" & chosen as text
else if errorNumber = 5016 then
display dialog "Catch error 5016"
end if -- ignore all other errors
end try
Edit:
To mount volumes without error dialog boxes use the shell commands mount_afp or mount_smbfs.
This is an example for AFP.
It checks if the volume is already mounted. If not it creates the mount point "manually" and mounts the volume. On failure the mount point will be deleted. The SMB version works accordingly.
property server : "myServer.local"
property serverVolume : "Server"
property user : "user"
property pass : "pass"
set isMounted to serverVolume is in (do shell script "/bin/ls /Volumes") or mountAFP(user, pass, server, serverVolume)
if isMounted then
-- do something
end if
on mountAFP(user_name, pass_word, thehost, theVolume)
set theAddress to quoted form of ("afp://" & user_name & ":" & pass_word & "#" & thehost & "/" & theVolume)
set mountpoint to quoted form of ("/Volumes/" & theVolume)
try
do shell script "/bin/mkdir " & mountpoint & "; /sbin/mount_afp " & theAddress & space & mountpoint
return true
on error e
log e
do shell script "/bin/rm -r " & mountpoint
return false
end try
end mountAFP
I've got an AutoIt script that goes to a website, logs in, and navigates to another page. But _IENavigate is throwing an error that isn't in the documentation . The output to the following code is: "Here is what the flag is: -1 and the error flag is: 0"
But you'll see in the documentation that only error codes 1-9 exist, there is no flag for #error = 0...
I do notice that the browser does navigate to the second page but when I try getting all the tags on the page, it doesn't find them. Please help! :)
Local $oIE = _IE
Create("http://www.example.com/")
_IELoadWait($oIE)
Local $j_username = _IEGetObjByName($oIE, "username")
_IEFormElementSetValue($j_username, $sUser)
Local $j_password = _IEGetObjByName($oIE, "password")
_IEFormElementSetValue($j_password, $sPass)
Local $login_submit = _IEGetObjByName($oIE, "button")
_IEAction($login_submit, "click")
_IELoadWait($oIE)
$iFlag = _IENavigate($oIE, "http://www.example.com/page2", 1)
ConsoleWrite( "Here is what the flag is: " & $iFlag & " and the error flag is: " & #error & #CRLF)
_IELoadWait($oIE)
Turns out the documentation says:
This function always returns a value of -1. This is because the navigate method has no useful return value and therefore nothing can be implied from it.
It's a feature, not a bug. :)
Although I found similar problems none could provide a sufficient answer to my problem.
I came across an older VBScript that is used for communication and synchronization with Outlook. Somebody (not me) gets a strange error when running the script:
Script:
"Some path"
Line: 286
Character: 9
Error: Invalid process or invalid argument
Code: 800A0005
Source: Runtime Error in VBScript
Now what I want to know is WHY that error occurs. Just a general interpretation of the error. But first here is the relevant code:
Set refnum = open_outputfile(outputfileverz & outputfilename_short)
If (errcode = 0) Then
If (check_textfile(refnum)) Then
refnum.WriteLine (strOutput)
errcode = close_outputfile(outputfileverz & outputfilename_short, outputfilename_short, refnum)
If (errcode = 0) Then
If (logging) Then 'mit Protokollierung
If (check_textfile(logrefnum)) Then
logrefnum.WriteLine ("--- " & Now() & " ---" & crlf & outputfilename_short & ":" & crlf & strOutput)
If (gBAbort) Then
logrefnum.WriteLine (errtext(33)) ' Abbruch durch den Benutzer
End if
End If
End If
Else
ok = false
End If
Else
'errdetails = outputfileverz & outputfilename_short wurde schon in open_outputfile erstellt
ok = False
End If
End If
Line 286 is:
refnum.WriteLine (strOutput)
There are of course two other functions involved (open_outputfile/check_textfile) but they only return a document reference and check if it is a valid reference respectively.
Now I tried to reproduce the error and there is a problem: No matter what I do I don't get this error. I've tried empty references, empty strings, wrote all kind of variable types but I always get other errors.
Can anybody tell me how or why this happens?
In Access 2007, I have a form to add a new contact to a table:
RecSet.AddNew
RecSet![Code_Personal] = Me.txtCodePersonal.Value
RecSet![FName] = Me.TxtFName.Value
RecSet![LName] = Me.txtLName.Value
RecSet![Tel Natel] = Me.txtNatTel.Value
RecSet![Tel Home] = Me.txtHomeTel.Value
RecSet![Email] = Me.txtEmail.Value
RecSet.Update
This has worked so far, and the contact has successfully been aded. But I'm having two problems:
I want to display a messagebox to tell the user the contact was successfully added
If the contact was not successfully added because
A contact with this name already exists
A different issue
Then display a message box "Contact already exists" or "error occured" respectively.
My idea of doing this is:
If recSet.Update = true Then
MsgBox "Paolo Bernasconi was successfully added"
Else if RecSet![FName] & RecSet![LName] 'already exist in table
MsgBox "Contact already exists"
Else
MsgBox "An unknown error occured"
I know this code is wrong, and obviously doesn't work, but it's just to give you an idea of what I'm trying to achieve. Thanks for all your help in advance.
Add an error handler to your procedure.
On Error GoTo ErrorHandler
Then display the "success" notice to user immediately after updating the recordset.
RecSet.Update
MsgBox RecSet![FName] & " " & RecSet![FName] & _
" was successfully added"
If the update attempt fails, flow control passes to the ErrorHandler section.
ErrorHandler:
MsgBox "Oops!"
Undoubtedly you want something more refined than an "Oops!" message. A slick approach is to use a Select Case block to customize the response based on error number.
Determine whether the contact exists already before attempting to add it.
strCriteria = "Fname = '" & RecSet![FName] & "' AND LName = '" & _
RecSet![LName] & "'"
Debug.Print strCriteria
If DCount("*", "YourTable", strCriteria) > 0 Then
' do not attempt to add it again
MsgBox "Contact already exists"
Else
RecSet.AddNew
' and so forth
End If
Check the Debug.Print output in case I made a mistake when building strCriteria.
The intention here is to avoid the duplication error condition ... by only attempting to add a contact which doesn't exist. So that error should not happen, and any other errors will be dealt with by the error handler.