Use the Err.GetException method to create custom exceptions

The Visual Basic .NET Err.Raise method and the Throw command are (partially) compatible. For example, you can use a Try…End Try block to catch an error raised with the Err.Raise method, and you can use an On Error Resume Next and the Err object to neutralize and inspect an exception object created by the Throw command. The old and the new error trapping mechanisms don’t coexist well, though, and there are a few limitations. For example, you can’t have an On Error Resume Next statement and a Try…End Try block in the same procedure.

To assist you in porting existing applications to VB.NET, the Err object has been extended with the new GetException method, which returns the Exception object that corresponds to the current error. This feature lets you preserve old-style error handlers in those procedures that don’t lend themselves to an easy porting to the new syntax. This new method also enables such procedures them to correctly throw an exception object to their caller, where the exception can be processed using a Try block as usual:

Sub OldStyleErrorHandlerProc()    On Error Goto ErrorHandler    ' Cause a division by zero error,    Dim x, y As Integer    y = 1  x    Exit SubErrorHandler:    ' Add here clean up code as necessary.    ' ...    ' Then report the error to the caller as an Exception object.    Throw Err.GetExceptionEnd Sub

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a