Does .NET has an Exception that similar to Delphi’s EAbort ?
Currently, I define my own “AbortProcess” inheriting Exception.
Together with My.Application.UnhandledException handler that ignoring “AbortProcess”
I’m still wondering if similar mechanic in .NET is already exists.
Class AbortProcess
Inherits System.Exception
End Class
Sub Abort()
Throw New AbortProcess()
End Sub
Sub AppDomain_UnhandledException(ByVal sender As Object, ByVal e As ApplicationServices.UnhandledExceptionEventArgs)
If TypeOf e.Exception Is AbortProcess Then
e.ExitApplication = False
End If
End Sub
Sub PerformActions()
Action1()
If Not Action2() Then
Abort()
End If
Action3()
...
End Sub
How does typical .NET developer handle this use case ?
UPDATED:
For some reasons a number of people down vote this question, unfortunately, without giving any comment. The only reason I can figure out is that they might believe Exception should never be used to control program flow; and I tend to agree with that. However, I recently study ANTLR and find that they indeed use custom Exception (RecognitionException) as control flow construct. Together with Python’s StopIteration usage, I believe that using Exception as control flow construct is actually already being used broadly. It’s just not being standardize like in Delphi VCL.
There are two qualities that define Delphi’s
EAbortexception class.EAbortand its descendants and does not display the usual message box when it catches such an exception.It looks like the code you’ve proposed accomplishes the second part. You can configure Visual Studio for the first part; refer to the answer to another Stack Overflow question, Is there a better way to get Visual Studio to ignore try/catch in debug mode? I’m not aware of any exception class already designated for that.
The
EAbortexception is meant to make the program stop running whatever event or message handler it’s running and resume at the main message loop. In order for that to really work, though, all your other code needs to be written to handle exceptions properly. That is, they need to usefinallysections to keep themselves in stable and consistent states, and they need to either rethrow or never catch exceptions that they aren’t really capable of fixing.