Theory
वह Crash जो आप पहले मिल चुके हैं
Cashier price "12x" type करता है, ShopKeeper CDec(txtPrice.Text) run करता है, और .NET एक exception raise करता है; unhandled, app queue के बीच में मर जाता है।
यह story आप जानते हैं: BookBridge same तरीके से BCA403 में मरा था जब तक try/catch नहीं आया। Java की grammar यहाँ almost word for word transfer होती है।
पर VB.NET कुछ ऐसा carry करता है जो Java के पास कभी नहीं था: errors handle करने का एक दूसरा, पुराना तरीका, classic VB6 से inherited। Syllabus चाहता है आप नए में fluent हों और पुराने को READ कर सकें, क्योंकि पुराना अभी भी असली shop software में lurk करता है।
Theory
Structured Handling: Modern School
Structured exception handling Try/Catch/Finally है, block-scoped और typed:
- risky lines Try में जाती हैं
- हर Catch उस exception type को नाम देता है जिसे यह handle करता है, specific पहले, general आख़िर में (child पहले parent, वही BCA403 वाला unchanged ordering rule)
- Finally हर exit पर run होता है: success, failure, यहाँ तक कि एक Return पर भी
- Throw एक exception raise करता है; ex.Message एक को describe करता है
Words Java के हैं, बस braces की जगह End Try के साथ; BookBridge में practice किया हर trace (A-C-D jump) identically replay होता है।
Practical
दोनों Schools, वही Bad Price
' STRUCTURED: the modern standard
Private Sub btnAdd_Click(sender As Object, e As EventArgs) _
Handles btnAdd.Click
Try
Dim price As Decimal = CDec(txtPrice.Text) ' "12x" throws here
lblStatus.Text = "Added at " & price
Catch ex As InvalidCastException
errProv.SetError(txtPrice, "Price must be a number")
Catch ex As Exception ' general net, LAST
MessageBox.Show("Unexpected: " & ex.Message)
Finally
txtPrice.SelectAll() ' runs either way
End Try
End Sub
' UNSTRUCTURED: legacy VB6 style, read-only knowledge
Sub OldSchoolAdd()
On Error GoTo Handler
Dim price As Decimal = CDec("12x") ' jumps to Handler on failure
Exit Sub
Handler:
MsgBox("Error " & Err.Number & ": " & Err.Description)
End Sub
Theory
Unstructured Handling: Old School
.NET से पहले, VB errors procedure-wide handle होते थे:
- On Error GoTo label: उस line से आगे, procedure का ANY error label पर jump करता है
- Resume Next (एक handler के अंदर): वापस जाकर failing line के बाद continue कीजिए
- Err object: Err.Number और Err.Description latest error describe करते हैं
- On Error Resume Next: वह infamous वाला: हर error चुपचाप skip होता है, procedure जो भी half-done state बचा हो उसके साथ आगे बढ़ता जाता है
एक hard rule: एक procedure दोनों styles mix नहीं कर सकता; एक ही Sub में Try और On Error एक compile error है।
At a glance
Structured बनाम Unstructured (Exam Table)
| Aspect | Structured (Try/Catch) | Unstructured (On Error) |
|---|---|---|
| Scope | बिल्कुल वह block जो आप wrap करते हैं | On Error line के बाद पूरा procedure |
| Error identity | Typed exception objects (ex As FormatException) | एक Err object, kinds के लिए numbers |
| Cleanup | Finally, guaranteed | Manual, miss करना आसान |
| Readability | Handling risk के बगल में बैठता है | GoTo दूर वाले labels पर jump करता है |
| Status | .NET standard | Legacy VB6 compatibility |
Quiz
एक procedure On Error Resume Next से शुरू होता है। बीच में, CDec("12x") fail होता है। Program क्या करता है?
- Default error dialog दिखाता है और procedure रोक देता है
- Failing line silently skip करता है और अगले statement से continue करता है
- Procedure की पहली line पर वापस jump करके retry करता है
- Text को 12 में convert कर देता है, invalid characters ignore करते हुए
Show the answer
Failing line silently skip करता है और अगले statement से continue करता है
Resume Next एक policy की तरह मतलब रखता है हर error swallow हो जाता है: assignment बस होता ही नहीं, price अपनी पुरानी value रखता है (एक fresh Decimal के लिए 0), और procedure आगे बढ़ता रहता है, ग़लत numbers के साथ: bills जो चुपचाप zero हैं। वही silence है जिस वजह से यह construct infamous है, और वजह है option A ग़लत क्यों है: उस dialog को suppress करना ही exactly यह करता है। Option C एक retry loop invent करता है। Option D Val()'s forgiving parse describe करता है, एक बिल्कुल अलग tool: error HANDLING कभी आपका data edit नहीं करता।
Think first
अपने Java Reflexes Translate कीजिए
BCA403 से आप जानते हैं: try, catch child-before-parent, finally हमेशा run होता है, e.getMessage()। Tap करने से पहले, हर एक की VB.NET spelling लिखिए।
Show the answer
try बनता है Try ... End Try; catch (FormatException e) बनता है Catch ex As FormatException, और ordering rule intact रहता है: general Catch ex As Exception के ऊपर specific Catch, वरना general वाला पहले सब swallow कर लेता है। finally है Finally, वही guarantee, वही runs-even-on-Return behaviour। e.getMessage() है ex.Message: अब एक property, कोई brackets नहीं। Knowledge का एक import, zero नए concepts: आपके BCA में यह exact machinery इस्तेमाल करने वाली चौथी language, जो कि machinery सीखने का point ही है।
Watch out
जहाँ Marks और Bills खो जाते हैं
नए code में On Error Resume Next: quiz वाला silent-zero bill; एक exam में, इसे dangerous option की तरह नाम दीजिए और बताइए क्यों: errors IGNORE होते हैं, handle नहीं।
General Catch पहले: specific वालों के ऊपर Catch ex As Exception उन्हें unreachable बना देता है: Java जो compile-time complaint देता था वही यहाँ भी।
Schools Mix करना: एक procedure में Try और On Error compile नहीं होता; एक चुनिए (Try चुनिए)।
Theory
Unit 3 बंद होती है; Till Trustworthy है
Error handling आख़िरी brick था: ShopKeeper का face (controls, containers, grid), इसके tray workers, इसके menus और dialogs, और अब जब input ग़लत जाए इसका composure: expected mistakes के लिए ErrorProvider, बाकी के लिए Try/Catch, cleanup के लिए Finally। Unit 4 register पूरी तरह बदल देती है: इस screen के पीछे Item और Bill CLASSES, VB.NET की OOP: Me और MyBase जहाँ Java ने this और super कहा था। आपकी BCA403 वाली muscle memory अब पूरी unit के लिए pay करने वाली है।
Summary
Key takeaways
- Structured: Try / typed Catch blocks (specific पहले) / Finally (हमेशा run होता है) / End Try; Throw raise करता है, ex.Message describe करता है।
- Unstructured (legacy): On Error GoTo label, Resume Next, Err object (Number, Description)।
- On Error Resume Next चुपचाप हर error skip करता है: इसे dangerous नाम दीजिए, नए code में कभी मत लिखिए।
- एक procedure दोनों styles mix नहीं कर सकता।
- Structured scope, typing, guaranteed cleanup और readability पर जीतता है: exam comparison table।
- Java translation: try=Try, catch=Catch ex As Type, finally=Finally, getMessage()=ex.Message।
- Memory hook: Try handle करता है, Resume Next hide करता है।