Theory
crash જે તમે પહેલા meet કર્યો છે
Cashier price "12x" types કરે છે, ShopKeeper CDec(txtPrice.Text) run કરે છે, અને .NET exception raise કરે છે; unhandled, app mid-queue માં dies થાય છે.
તમે આ story ને જાણો છો: BookBridge same રીતે BCA403 માં died હતું જ્યાં સુધી try/catch arrive ન થયું. Java grammar અહીં almost word for word transfer થાય છે.
પણ VB.NET એ કંઈક carry કરે છે જે Java ક્યારેય નહોતું: errors ને handle કરવાની second, older way, classic VB6 થી inherited. syllabus તમને new માં fluent અને old ને READ કરવા સક્ષમ ઇચ્છે છે, કારણ કે old હજુ real shop software માં lurks કરે છે.
Theory
Structured handling: modern school
Structured exception handling એ Try/Catch/Finally છે, block-scoped અને typed:
- risky lines Try માં જાય છે
- દરેક Catch exception type ને name કરે છે જે તે handle કરે છે, specific first, general last (child before parent, BCA403 ordering rule unchanged)
- Finally દરેક exit પર runs કરે છે: success, failure, even Return
- Throw exception raise કરે છે; ex.Message એક ને describe કરે છે
words Java ના છે End Try સાથે braces ને બદલે; દરેક trace જે તમે BookBridge માં practise કર્યું (A-C-D jump) identically replays થાય છે.
Practical
બંને schools, same 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 કરે છે
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") ' failure પર Handler પર jumps કરે છે
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 થી on, procedure માં કોઈ પણ error label પર jumps થાય છે
- Resume Next (handler ની અંદર): failing line પછી back જઈને continue કરો
- Err object: Err.Number અને Err.Description latest error ને describe કરે છે
- On Error Resume Next: infamous one: દરેક error silently skip થાય છે, procedure whatever half-done state remains સાથે marches on
એક hard rule: procedure બંને styles ને mix નથી કરી શકતું; same Sub માં Try અને On Error એ compile error છે.
At a glance
Structured vs unstructured (exam table)
| Aspect | Structured (Try/Catch) | Unstructured (On Error) |
|---|---|---|
| Scope | Exact block જે તમે wrap કરો | On Error line પછી આખું procedure |
| Error identity | Typed exception objects (ex As FormatException) | એક Err object, kinds માટે numbers |
| Cleanup | Finally, guaranteed | Manual, easy to miss |
| Readability | Handling risk ની બાજુમાં sits કરે છે | GoTo distant labels પર jumps કરે છે |
| Status | .NET standard | Legacy VB6 compatibility |
Quiz
પ્રક્રિયા On Error Resume Next સાથે શરૂ થાય છે. અડધે, CDec("12x") fail થાય છે. program શું કરે છે?
- default error dialog show કરે છે અને procedure ને stop કરે છે
- failing line ને silently skip કરે છે અને next statement સાથે continue કરે છે
- procedure ની first line પર પાછું jumps કરે છે અને retry કરે છે
- invalid characters ને ignore કરીને text ને 12 માં convert કરે છે
Show the answer
failing line ને silently skip કરે છે અને next statement સાથે continue કરે છે
Resume Next policy તરીકે એટલે કે દરેક error swallow થાય છે: assignment simply happen નથી થતું, price તેનું previous value retain કરે છે (fresh Decimal માટે 0), અને procedure આગળ વધે છે, wrong numbers અને all: bills silently zero. એ silence છે જે એટલે construct infamous છે, અને શા માટે option A wrong છે: તે dialog ને suppress કરવું એ exactly એ છે જે તે કરે છે. Option C retry loop ને invent કરે છે. Option D Val() ના forgiving parse ને describe કરે છે, entirely different tool: error HANDLING ક્યારેય તમારા data ને edit નથી કરતું.
Think first
તમારા Java reflexes ને translate કરો
BCA403 થી તમે જાણો છો: try, catch child-before-parent, finally હંમેશા runs કરે છે, e.getMessage(). tap કરતા પહેલા, દરેક નું VB.NET spelling લખો.
Show the answer
try એ Try ... End Try બને છે; catch (FormatException e) એ Catch ex As FormatException બને છે, અને ordering rule intact survive કરે છે: specific Catch general Catch ex As Exception ની ઉપર, અથવા general one everything ને પહેલા swallow કરે છે. finally એ Finally છે, same guarantee, same runs-even-on-Return behaviour. e.getMessage() એ ex.Message છે: હવે property, no brackets. knowledge નો એક import, zero new concepts: fourth language તમારા BCA માં જે આ exact machinery ને use કરે છે, જે rather point છે machinery learn કરવાનો.
Watch out
જ્યાં marks અને bills go missing થાય છે
નવા code માં On Error Resume Next: quiz નો silent-zero bill; exam માં, તેને dangerous option તરીકે name કરો અને કહો શા માટે: errors ને IGNORED કરવામાં આવે છે, handled નહીં.
General Catch first: Catch ex As Exception specific ones ની ઉપર તેમને unreachable બનાવે છે: same compile-time complaint જે Java આપ્યું.
schools ને Mixing કરવી: એક procedure માં Try અને On Error compile નથી થતું; એક ને pick કરો (Try ને pick કરો).
Theory
Unit 3 closes; till trustworthy છે
Error handling છેલ્લો brick હતો: ShopKeeper નું face (controls, containers, grid), તેના tray workers, તેના menus અને dialogs, અને હવે તેની composure જ્યારે input wrong થાય છે: expected mistakes માટે ErrorProvider, બાકી ના માટે Try/Catch, cleanup માટે Finally. Unit 4 register completely change કરે છે: આ 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 first) / Finally (હંમેશા runs) / End Try; Throw raise કરે છે, ex.Message describe કરે છે.
- Unstructured (legacy): On Error GoTo label, Resume Next, Err object (Number, Description).
- On Error Resume Next દરેક error ને silently skip કરે છે: તેને dangerous તરીકે name કરો, new code માં ક્યારેય ન લખો.
- એક procedure બંને styles ને mix નથી કરી શકતું.
- Structured scope, typing, guaranteed cleanup અને readability પર wins કરે છે: exam comparison table.
- Java translation: try=Try, catch=Catch ex As Type, finally=Finally, getMessage()=ex.Message.
- Memory hook: Try handle કરે છે, Resume Next hide કરે છે.