C++ C++ C# C# ASP.NET Security ASP.NET Security ASM ASM Скачать Скачать Поиск Поиск Хостинг Хостинг  
  Программа для работы с LPT портом...
Язык: .NET — ©Alexey...
  "ASP.NET Atlas" – AJAX в исполнении Micro...
Язык: .NET — ©legigor@mail.ru...
  "Невытесняющая" Многопоточность...
Язык: C/C++ — ©...
  Update World C++: Сборник GPL QT исходников
  Весь сайт целиком можно загрузить по ссылкам из раздела Скачать
Дебетовая карта Home Credit [CPS] RU

 Adding Try/Catch Block using AddIn For Visual Studio .NET 2003 / VS.NET Addins / C#


This is an extensibility of VS.NET 2003.This add-in adds try/catch blocks inside your method. This will save development time. Basically it checks whether it is a method and also whether a try/catch block already exists. If not it adds a try/catch inside your method. Usually programmers forget to write try/catch during development time and at the slast minute they will add it in all the pages. That time this will be very useful .They can just execute this add-in which will automatically add try/catch blocks inside your method.

Installation Steps

  1. Run the Setup.exe from the AddTryCatchInstall.zip.
  2. Select the installation folder where you want to install the setup.
  3. Click Close to finish the installation.

Using the Tool

After you install the add-in, it will be listed in the Tools menu automatically. To use this you have to place your cursor inside the method and click this add-in. Adding to the ToolBar: to add it to your toolbar, go to Tools -> Customize -> Select Command tab, then choose AddIn and drag AddTryCatch to the toolbar and drop it.

Source Code Overview

You have to declare this struct which contains the caret position, function name, start and end position of the function.

private struct SFunction
    public string FuncName;
    public int StPtr;
    public int EndPt;
    public int CaretPos;
    public bool SearchDone;
private SFunction OneFunction;

This method checks for the active document and calls GetWholeProcedure() to add the try/catch block. Add AddingTryCatch() method call in the Exec method (e.g.) handler = AddingTryCatch();

public bool AddingTryCatch()
    if (applicationObject.ActiveDocument !=null)
    return true;

Code listed below checks for the function in the current window and fetches the function where the cursor is placed. Then it checks if try/catch block exists already. Then it inserts a try at the start of the function and catch/finally at the end of the function.

public void GetWholeProcedure()
    TextSelection ts = (TextSelection) applicationObject.ActiveDocument.Selection;
    EditPoint ep = ts.ActivePoint.CreateEditPoint();
    OneFunction.SearchDone= false;

    // if we found the function, get the code
    if (OneFunction.SearchDone)
        string s = ep.GetLines(ep.Line,OneFunction.EndPt+1);
        // Check for Try/Catch Exists
        int found = s.IndexOf("try"); 
        int foundcatch = s.IndexOf("catch("); 
        int position = 2;
        // If not Try/Catch Exists
        if(found == -1 || foundcatch == -1 )
            TextSelection ts1 = 
            // Sets the Insertion point based on webservice or webform file
                      != -1 && s.IndexOf("[WebMethod]") != -1 )  
                position = 3;
                position = 2;
            ts1.GotoLine((OneFunction.StPtr + position),true);
            // Inserts Try at start of the function
            EditPoint e =ts1.TopPoint.CreateEditPoint();
            // Indents the lines 
            int currentposition = OneFunction.StPtr + position + 1 ;
            for(int intLoop = 1 ; intLoop <=(OneFunction.EndPt - 
                ts1.GotoLine((currentposition + intLoop),true);
                if(ts1.Text.Equals("") != true )
            // Goes to the end of the function and inserts Catch/Finally Block.
            e.Insert("\t\t\t}\n\t\t\tcatch(Exception ex)\n\" + 
                     "t\t\t{\n\t\t\t\tthrow ex;\n\t\t\t}\n\" + 
            System.Windows.Forms.MessageBox.Show("Try/Catch Block already exists"); 

The below functions are the major things which find out the function in the active window. It checks for the function in the caret position and returns the starting and ending point of the function and sets the searchDone flag to true. Some of the extensibility classes used are:

  • ProjectItem - Represents an item in a project.
  • FileCodeModel - Allows access to programmatic constructs in a source file.
  • CodeElements - Represents a code element or construct in a source file.
  • CodeNamespace - Represents a namespace construct in a source file.
  • CodeClass - Represents a class in source code.
// enumerates all classes in all namespaces in a window 
private void SearchForFunctionInCurrentWindow()
    ProjectItem pi = (ProjectItem) applicationObject.ActiveWindow.ProjectItem;
    FileCodeModel fcm = pi.FileCodeModel;

    if ( fcm!=null) 
private void GetFunction(CodeElements elements)
    // Looks for the function and returns the code.
    CodeElements members;
    foreach (CodeElement elt in elements)
            return ;

        if (elt.Kind==vsCMElement.vsCMElementFunction )
            if(OneFunction.CaretPos>= OneFunction.StPtr && 
               OneFunction.CaretPos <= OneFunction.EndPt)
            members = GetMembers(elt);
            if (members != null )
private CodeElements GetMembers(CodeElement elt)
    CodeElements members = null ;
    if(elt!= null)
        switch ( elt.Kind)
            case (vsCMElement.vsCMElementNamespace ):
                CodeNamespace cdeNS = (CodeNamespace) elt;
                members = cdeNS.Members;
                break ;
            case (vsCMElement.vsCMElementClass):
                CodeClass cdeCL = (CodeClass) elt;
                members = cdeCL.Members;
            case (vsCMElement.vsCMElementFunction):
                // functions dont have members
    return members;


Hope this tool would be useful. Good day...

Дебетовая карта Home Credit [CPS] RU