Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have tried so many times but I am not able to print error line number in Vb script. I can get Error Number and Error Description but I do not get where error is exactly occurred ...

What I have tried:

On Error Resume Next

Dim a, b, c
a = 10
b = 0
c = a / b

If Err.Number <> 0 Then
    Dim fso, file, filePath
    Set fso = CreateObject("Scripting.FileSystemObject")
    filePath = fso.GetParentFolderName(WScript.ScriptFullName) & "\error.txt"
    Set file = fso.CreateTextFile(filePath, True)
    For i = 1 To 3

            file.WriteLine "Error on line " & i & ": " & Err.Number & " - " & Err.Description
            Exit For

    Next
    file.Close
    Set file = Nothing
    Set fso = Nothing
End If

On Error GoTo 0
Posted
Updated 3-May-23 1:11am

To get the line number where the error occurred, you need to use the Erl function. However, this will only work if your code has line numbers; otherwise, the function will always return 0.
VBA
On Error Resume Next

10: Dim a, b, c
20: a = 10
30: b = 0
40: c = a / b

If Err.Number <> 0 Then
    Dim fso, file, filePath
    Set fso = CreateObject("Scripting.FileSystemObject")
    filePath = fso.GetParentFolderName(WScript.ScriptFullName) & "\error.txt"
    Set file = fso.CreateTextFile(filePath, True)
    file.WriteLine "Error on line " & Erl & ": " & Err.Number & " - " & Err.Description
    file.Close
    Set file = Nothing
    Set fso = Nothing
End If

On Error GoTo 0
 
Share this answer
 
Comments
Keyur Raval 2022 3-May-23 6:42am    
I ran above code and it gives me error on Line 3
Line: 3
Char: 1
Error: Expected Statement
Code: 800A0400
Source: Microsoft VBScript Compile Error
Richard Deeming 3-May-23 6:48am    
Ah, that doesn't work in VBScript, only in VBA. Looks like VBScript doesn't provide any way to get that information.

Is there any reason you haven't moved to a more modern language? As far as I can see, VBScript hasn't been updated since 2009. Either VB.NET or PowerShell would be a better choice.
Keyur Raval 2022 9-May-23 2:24am    
Yes there is a reason behind that...but I can't explain all the details here. However you provided a good example of VBA. Thank you.!
First off, remove the execrable On Error Resume Next from your entire code - all it does is hide errors so either you don't get the effects you wanted and don't know why (and since you have thrown away any error information that might help you, you have no way to know or find out), or they become so extreme as a result that something gets serious corrupted and you actually notice that (and by then you have no idea where the problem started!)

Then use the debugger to find out what exactly is going on, and if any errors are being thrown. If they are, then they will show you where, and you can look at the variables to find out why that exception was thrown.
If they aren't, then you can follow your code through as it runs, and see exactly what it is doing and what data it is processing.

Sorry, but we can't do any of that for you!
 
Share this answer
 
Comments
Keyur Raval 2022 3-May-23 5:51am    
I just want to print error line number in notepad.. can you please give me VBScript code for that?
Dave Kreskowiak 3-May-23 8:09am    
You're not doing anything in "notepad". You're writing to a text file, not a "notepad" file. You load text files into Notepad to read/edit them.

VBScript does not support getting line numbers from errors they occur on. You can get the error Number, Source, and Description. That's all.
Quote:
I can get Error Number and Error Description but I do not get where error is exactly occurred ...

It happens that you have extremly carefull to ignore the error, thanks to this line.
VB
On Error Resume Next

Remove to line and your program will crash on error.
Advice: learn about "on error" and error handlers.
 
Share this answer
 
Comments
Keyur Raval 2022 4-May-23 5:42am    
I have intentionally used above line so that I can print error number and error description in notepad... Just I want to print error line number along with it.
Patrice T 4-May-23 6:48am    
Look ay how to use error handlers.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900