locked
Custom Pre compile directives RRS feed

  • Question

  • Hi,
    we're in a process of migrating our VB6 project to C# .NET (yep a very challenging project).
    As part of our investigation on error handling we need to solve the On Error Resume Next , Resume and Resume Next instruction.
    While we found acceptable solution for On Error Goto we did not find an elegant solution for Resume Next and Resume instraction.
    Here is the summary of our finding:

    Resume/Resume Next in catch block

    Visual Basic also supports the following pattern:

     

    Private Sub Test()

     

        On Error GoTo Func_Err

     

        Debug.Print("1")

        Err.Raise(vbObjectError + 512 + 1)

        Debug.Print("2")

     

    Func_End:

        Exit Sub

     

    Func_Err:

        DoSomething()

        Resume Next

     

    End Sub

     

    The following is a sugested solution:

    private void Test()

    {

     

        #region "Resume Next Declaration"

        int errorIndex = 0;

    Label_StartPoint:

        #endregion

     

        try

        {

            #region "Resume Next Switch Block"

            switch (errorIndex + 1) // For Resume Use switch (errorIndex)

            {

                case 1:

                    goto Label_1;

                case 2:

                    goto Label_2;

                case 3:

                    goto Label_End;

                default:

                    goto Label_1;

     

            }

            #endregion

     

        Label_1:

            errorIndex = 1;

            Console.WriteLine("1");

            throw new Exception("Dummy Exception!");

     

        Label_2:

            errorIndex = 2;               

            Console.WriteLine("2"); 

        Label_End:            

          

        }

        catch (Exception)

        {

            DoSomthing();

            goto Label_StartPoint;            

        }

     

    }
    While this solution will work it is not a very elegant solution.
    Is it prossible to set somekind of directive to the compiler where i can write the code without the error handling and in the pre compile time to change the according the pattern above?

    for those who curios how we solved the On Error Resume Next here is our solution:

    On Error Resume Next

    In order to support On Error Resume Next functionality we suggest using the following pattern:

     

    namespace Mv.VBCompatibility

    {

        public static class ErrorManager

        {

            public delegate void Proc();

     

            public static void ResumeNext(Proc func)

            {

     

                try

                {

                    func();

                }

                catch (Exception exp) {}

            }

     

            public static void ResumeNext(Proc func, out Exception e)

            {

                e = null;

     

                try

                {

                    func();

                }

                catch (Exception exp)

                {

                    e = exp;

                }

            }

        }

          ...

    }

     

    public void TestOnErrorResumeNext()

    {

        Exception exp = null;

        FirstClass classThrows = null;

     

        Error.ResumeNext(() => { classThrows = null; });

        Error.ResumeNext(() => { classThrows.ProcessFirst(); }, out exp);

        if (exp != null)

        {

            Console.WriteLine("Same functionality as If Err.Number <> 0 ");

        }

    }

    Friday, November 14, 2008 11:56 AM

All replies

  • No, there's no such directive.

    And I wouldn't recommend using catch all exception blocks like you have in your suggested solution. It's better to just catch the exceptions you know how to handle.
    Mattias, C# MVP
    Friday, November 14, 2008 1:15 PM
  • Check this blog post.   Not sure why you switched from VB6 to C# instead of VB.NET.  But reproducing mistakes like On Error Resume Next is not going to get you a better end result.

    Hans Passant.
    Friday, November 14, 2008 1:23 PM
  • We know that migrating to VB.NET is much easier than C# but there are several reasons we want to migrate to C#:

    • To enforce our developers to think Object oriented. I saw several developers that wrote in VB.NET same way as in VB6 they did not understand that it is totally different platform. This is new object oriented language in new platform and it will be easier for our developers to understand that.
    • It is easier to recruit new good developers.
    • It is refreshing for our developers to work in new language.
    • From marketing point view it has some advantage to state that our code base is C# and not VB whether we agree or not.

    Regarding the solution we suggested: Please not that we're not going to rewrite our code base from scratch since it is very complicated products with 1M LOC (line of code).
    We are going to migrate our code with an automatic migration tool so the only thing we can do is to set a pattern for the On Error Resume Next and not specific implementation for each use case.
    Later on we will try to refactor some code and On Error Resume Next, Resume Next, Resume is good candidate.
    So as i understand we cannot hook to the compiler and change our code before the compilation where we can automatically change our code before the actual compilation, is it correct?

     

    Saturday, November 15, 2008 8:46 PM