2010-02-04 10 views
49

मैं कुछ वीबीस्क्रिप्ट उदाहरणों में आया, और मैंने मूल रूप से स्क्रिप्ट की शुरुआत में On Error Resume Next स्टेटमेंट देखा।"त्रुटि फिर से शुरू करें" कथन क्या करता है?

यह क्या करता है?

+6

यह वाक्यविन्यास का एक बहुत शक्तिशाली, लेकिन खतरनाक बिट है। इसका उपयोग करके बहुत सावधान रहें। – Nate

+2

अब यह और अधिक समझ में आता है। कुछ फ़ंक्शंस के बाद जो त्रुटि में समाप्त हो सकते हैं। उनके पास चेकएरर नामक एक समारोह है। – Omar

उत्तर

65

यह मूल रूप से प्रोग्राम को बताता है जब आपको कोई त्रुटि आती है तो अगली पंक्ति में जारी रहें।

21

जब कोई त्रुटि होती है, तो स्क्रिप्ट को बाधित किए बिना निष्पादन अगली पंक्ति पर जारी रहेगा।

9

इसका मतलब है, जब लाइन पर कोई त्रुटि होती है, तो यह स्क्रिप्ट को निरस्त किए बिना निष्पादन जारी रखने के लिए vbscript कह रहा है। कभी कभी, On Error, एक Sub कोड ब्लॉक में इस तरह निष्पादन के प्रवाह, कुछ परिवर्तन करने के लिए Goto लेबल इस प्रकार अब आप जानते हैं क्यों और कैसे GOTO के उपयोग स्पेगेटी कोड में परिणाम कर सकते हैं:

 
Sub MySubRoutine() 
    On Error Goto ErrorHandler 

    REM VB code... 

    REM More VB Code... 

Exit_MySubRoutine: 

    REM Disable the Error Handler! 

    On Error Goto 0 

    REM Leave.... 
    Exit Sub 

ErrorHandler: 

    REM Do something about the Error 

    Goto Exit_MySubRoutine 
End Sub 
+10

वीबीस्क्रिप्ट 'त्रुटि गोटो लेबल' वाक्यविन्यास का समर्थन नहीं करता है, केवल 'त्रुटि गोटो 0' पर। – Helen

32

यह ध्यान देने योग्य है कि जब भी On Error Resume Next प्रभाव में है, तब भी त्रुटि उत्पन्न होती है जब त्रुटि उत्पन्न होती है, इसलिए आप अभी भी सी-शैली त्रुटि प्रबंधन कर सकते हैं।

On Error Resume Next 

DangerousOperationThatCouldCauseErrors 

If Err Then 
    WScript.StdErr.WriteLine "error " & Err.Number 
    WScript.Quit 1 
End If 

On Error GoTo 0 
3

त्रुटि वक्तव्य पर - निर्दिष्ट करता है कि जब रन-टाइम त्रुटि होती है, तो कथन के तुरंत बाद नियंत्रण कथन पर जाता है। एरर ऑब्जेक्ट कितनी बार पॉप्युलेट हो गया। (एरर नम्बर, एरर काउंटर इत्यादि)

0

त्रुटि फिर से शुरू करने का मतलब है कि त्रुटि पर, यह फिर से शुरू करने के लिए अगली पंक्ति में फिर से शुरू हो जाएगा।

उदा। यदि आप कोशिश ब्लॉक का प्रयास करते हैं, तो यह त्रुटि स्क्रिप्ट को रोक देगा यदि कोई त्रुटि हुई है

+0

कोशिश/पकड़ का उपयोग वीबीए (एक्सेल 2016) और न ही वीबीस्क्रिप्ट में मेरे लिए काम नहीं करता है। – user2415376

0

यह त्रुटि प्रबंधन को सक्षम बनाता है। निम्नलिखित आंशिक रूप से https://msdn.microsoft.com/en-us/library/5hsw66as.aspx

' Enable error handling. When a run-time error occurs, control goes to the statement immediately following the statement where the error occurred, and execution continues from that point. 
On Error Resume Next 

SomeCodeHere 

If Err.Number = 0 Then 
    WScript.Echo "No Error in SomeCodeHere." 
Else 
    WScript.Echo "Error in SomeCodeHere: " & Err.Number & ", " & Err.Source & ", " & Err.Description 
    ' Clear the error or you'll see it again when you test Err.Number 
    Err.Clear 
End If 

SomeMoreCodeHere 

If Err.Number <> 0 Then 
    WScript.Echo "Error in SomeMoreCodeHere:" & Err.Number & ", " & Err.Source & ", " & Err.Description 
    ' Clear the error or you'll see it again when you test Err.Number 
    Err.Clear 
End If 

' Disables enabled error handler in the current procedure and resets it to Nothing. 
On Error Goto 0 

' There are also `On Error Goto -1`, which disables the enabled exception in the current procedure and resets it to Nothing, 
' and `On Error Goto line`, which enables the error-handling routine that starts at the line specified in the required line argument. The line argument is any line label or line number. If a run-time error occurs, control branches to the specified line, making the error handler active. The specified line must be in the same procedure as the On Error statement, or a compile-time error will occur. 
संबंधित मुद्दे