Talk:Exception handling syntax: Difference between revisions

Content deleted Content added
Tisane (talk | contribs)
Rathgemz (talk | contribs)
Tag: repeating characters
Line 2:
 
Shouldn't proper BASIC error handling code always use the RESUME statement?
 
:I’m not sure about BASIC, but Visual Basic certainly should. The presented code (trying to emulate stuctured error handling with a lot of boilerplate code) just isn’t the VB way of doing it. What about the following? —[[User:Rathgemz|Rathgemz]] ([[User talk:Rathgemz|talk]]) 16:43, 4 October 2010 (UTC)
<syntaxhighlight lang="vb">
Const My_Error = vbObjectError + 132 ' It's the programmer's obligation to keep the error numbers unique
Function f(x as Integer) as Integer
' Idiom 1: Ignore errors or test explicitly after each statement
On Error Resume Next
Do_Something ' Exceptions from Do_Something are ignored, execution just continues with Do_Something_Else
Do_Something_Else
If Err.Number <> 0 Then
' handle errors in Do_Something_Else
End If
' Idiom 2: Error Handlers
On Error Goto Handler
...
Err.Raise My_Error ' Raising an exception
...
Good_Restart_Point:
...
Exit Function
Handler:
Select Case Err.Number
Case My_Error
' in case of My_Error, do something here
Resume Good_Restart_Point ' then continue with an appropriate statement
Case 58 ' File already exists
' delete file
Resume ' then restart the statement that failed
Case 6 ' Overflow
Result = &H7FFFFFFF ' set to maximum
Resume Next ' then continue with next statement
Case Else
' Propagate other errors to caller
Err.Raise Err.Number
End Select
End Function</syntaxhighlight>
:
 
== ''Else'' in C++ ==