A macro to protect a code section with a Try…Catch block

A macro to protect a code section with a Try…Catch block

When you’re working with the Visual Studio .NET code editor, typing a Try keyword plus the Enter key automatically creates the End Try statement. The following simple macro does something more interesting: it lets you select a portion of code and protect it with a Try…Catch…Finally…End Try block with a single key. Here’s the macro source code:

Sub CreateTryCatchFinally()    DTE.ActiveDocument.Selection.Cut()    DTE.ActiveDocument.Selection.Indent(2)    DTE.ActiveDocument.Selection.Text = "Try"    DTE.ActiveDocument.Selection.NewLine()    DTE.ActiveDocument.Selection.Paste()    DTE.ActiveDocument.Selection.NewLine()    DTE.ActiveDocument.Selection.DeleteLeft()    DTE.ActiveDocument.Selection.Text = "Catch ex as Exception"    DTE.ActiveDocument.Selection.NewLine()    DTE.ActiveDocument.Selection.DeleteLeft()    DTE.ActiveDocument.Selection.NewLine()    DTE.ActiveDocument.Selection.DeleteLeft()    DTE.ActiveDocument.Selection.Text = "Finally"    DTE.ActiveDocument.Selection.NewLine()End Sub

You should press Alt-F11 to bring up the macro IDE, and paste this code in an appropriate macro module. Then you should display the Tools-Options dialog and assign this macro a keyboard shortcut, for example the following (two keystrokes): Ctrl-Shift-V, Ctrl-Shift-T.

After you’ve done this, you can just select a block of code that you want to protect from exception and press the shortcut key, and see the Try block being created before your eyes. Note that you might need to reformat the code to achive the best indentation. If automatic indentation is on, you can do this by simply selecting the statements between Try and End Try keywords and press the Tab key once.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes