Exceptions/Catch an exception thrown in a nested call: Difference between revisions
Content added Content deleted
(Added Io entry) |
(Added FreeBASIC) |
||
Line 1,035: | Line 1,035: | ||
The second exception is not handled, and results in the program finishing |
The second exception is not handled, and results in the program finishing |
||
and printing a stack trace. |
and printing a stack trace. |
||
=={{header|FreeBASIC}}== |
|||
FreeBASIC does not support exceptions or the Try/Catch/Finally statement, as such. However, you can use the Err() function, together with an If (or Switch) statement, to provide somewhat similar functionality: |
|||
<lang freebasic>' FB 1.05.0 Win64 |
|||
Enum ErrorTypes |
|||
U0 = 1000 |
|||
U1 |
|||
End Enum |
|||
Function errorName(ex As ErrorTypes) As String |
|||
Select Case As Const ex |
|||
Case U0 |
|||
Return "U0" |
|||
Case U1 |
|||
Return "U1" |
|||
End Select |
|||
End Function |
|||
Sub catchError(ex As ErrorTypes) |
|||
Dim e As Integer = Err '' cache the error number |
|||
If e = ex Then |
|||
Print "Error "; errorName(ex); ", number"; ex; " caught" |
|||
End If |
|||
End Sub |
|||
Sub baz() |
|||
Static As Integer timesCalled = 0 '' persisted between procedure calls |
|||
timesCalled += 1 |
|||
If timesCalled = 1 Then |
|||
err = U0 |
|||
Else |
|||
err = U1 |
|||
End if |
|||
End Sub |
|||
Sub bar() |
|||
baz |
|||
End Sub |
|||
Sub foo() |
|||
bar |
|||
catchError(U0) '' not interested in U1, assumed non-fatal |
|||
bar |
|||
catchError(U0) |
|||
End Sub |
|||
Foo |
|||
Print |
|||
Print "Press any key to quit" |
|||
Sleep</lang> |
|||
{{out}} |
|||
<pre> |
|||
Error U0, number 1000 caught |
|||
</pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |