Handling Exception

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Franz

    #1

    Handling Exception

    try
    {
    }
    catch (ExceptionA A)
    {
    XXXXXXXXXXXXX
    }
    catch (ExceptionB B)
    {
    XXXXXXXXXXXXX
    }
    catch (Exception)
    {
    YYYYYYYYYYYYY
    }

    Actually the codes of hanlding both Exception A and B are same. I want to
    put it in a single catch block. Although I know that I can group the code
    into a function and call it in these two catch blocks, I don't want to have
    duplicated code. Also I don't want to give a inheritance relationship for
    both ExceptionA and ExceptionB. I

    Is there anything like the following? I want to do the following because if
    a large number of exception use the same hanlding code, then I can group
    them into a small number of brackets.

    try
    {
    }
    catch (ExceptionA A)
    catch (ExceptionB B)
    {
    XXXXXXXXXXXXX
    }
    catch (Exception)
    {
    YYYYYYYYYYYYY
    }

    Thanks.

    --
    Franz Wong
    My C++ and C# Site (Traditional Chinese) : http://www.franzwong.com



  • Andrew Gnenny

    #2
    Re: Handling Exception

    Here is my vision:

    try
    {
    }
    catch(Exception E)
    {
    if ((E is ExceptionA) || (E is ExceptionB))
    {
    XXXXXXXXXXXXX
    }
    else
    {
    YYYYYYYYYYYYY
    }
    }

    --
    Andrew Gnenny
    X-Unity Test Studio

    Bring the power of unit testing to VS .NET IDE


    "Franz" <honfung3@yahoo .com.hk> wrote in message
    news:unZlCsmhDH A.3204@TK2MSFTN GP11.phx.gbl...[color=blue]
    > try
    > {
    > }
    > catch (ExceptionA A)
    > {
    > XXXXXXXXXXXXX
    > }
    > catch (ExceptionB B)
    > {
    > XXXXXXXXXXXXX
    > }
    > catch (Exception)
    > {
    > YYYYYYYYYYYYY
    > }
    >
    > Actually the codes of hanlding both Exception A and B are same. I want to
    > put it in a single catch block. Although I know that I can group the code
    > into a function and call it in these two catch blocks, I don't want to[/color]
    have[color=blue]
    > duplicated code. Also I don't want to give a inheritance relationship for
    > both ExceptionA and ExceptionB. I
    >
    > Is there anything like the following? I want to do the following because[/color]
    if[color=blue]
    > a large number of exception use the same hanlding code, then I can group
    > them into a small number of brackets.
    >
    > try
    > {
    > }
    > catch (ExceptionA A)
    > catch (ExceptionB B)
    > {
    > XXXXXXXXXXXXX
    > }
    > catch (Exception)
    > {
    > YYYYYYYYYYYYY
    > }
    >
    > Thanks.
    >
    > --
    > Franz Wong
    > My C++ and C# Site (Traditional Chinese) : http://www.franzwong.com
    >
    >
    >[/color]


    Comment

    • Eric Gunnerson [MS]

      #3
      Re: Handling Exception

      Unless you have an inheritance relation, there is no way to do this.

      --
      Eric Gunnerson

      Visit the C# product team at http://www.csharp.net
      Eric's blog is at http://blogs.gotdotnet.com/ericgu/

      This posting is provided "AS IS" with no warranties, and confers no rights.
      "Franz" <honfung3@yahoo .com.hk> wrote in message
      news:unZlCsmhDH A.3204@TK2MSFTN GP11.phx.gbl...[color=blue]
      > try
      > {
      > }
      > catch (ExceptionA A)
      > {
      > XXXXXXXXXXXXX
      > }
      > catch (ExceptionB B)
      > {
      > XXXXXXXXXXXXX
      > }
      > catch (Exception)
      > {
      > YYYYYYYYYYYYY
      > }
      >
      > Actually the codes of hanlding both Exception A and B are same. I want to
      > put it in a single catch block. Although I know that I can group the code
      > into a function and call it in these two catch blocks, I don't want to[/color]
      have[color=blue]
      > duplicated code. Also I don't want to give a inheritance relationship for
      > both ExceptionA and ExceptionB. I
      >
      > Is there anything like the following? I want to do the following because[/color]
      if[color=blue]
      > a large number of exception use the same hanlding code, then I can group
      > them into a small number of brackets.
      >
      > try
      > {
      > }
      > catch (ExceptionA A)
      > catch (ExceptionB B)
      > {
      > XXXXXXXXXXXXX
      > }
      > catch (Exception)
      > {
      > YYYYYYYYYYYYY
      > }
      >
      > Thanks.
      >
      > --
      > Franz Wong
      > My C++ and C# Site (Traditional Chinese) : http://www.franzwong.com
      >
      >
      >[/color]


      Comment

      • Franz

        #4
        Re: Handling Exception

        Yes, you are right.
        Finally, I accept the following solution.

        catch (Exception e)
        {
        switch (e.GetType().To String())
        {
        case "ExceptionA ":
        case "ExceptionB ":
        Console.WriteLi ne("This is Exception A and B");
        break;
        }
        }

        --
        Franz Wong
        My C++ and C# Site (Traditional Chinese) : http://www.franzwong.com


        "Eric Gunnerson [MS]" <ericgu@online. microsoft.com> ¦b¶l¥ó
        news:uqvlLIshDH A.3636@tk2msftn gp13.phx.gbl ¤¤¼¶¼g...[color=blue]
        > Unless you have an inheritance relation, there is no way to do this.
        >
        > --
        > Eric Gunnerson
        >
        > Visit the C# product team at http://www.csharp.net
        > Eric's blog is at http://blogs.gotdotnet.com/ericgu/
        >
        > This posting is provided "AS IS" with no warranties, and confers no[/color]
        rights.[color=blue]
        > "Franz" <honfung3@yahoo .com.hk> wrote in message
        > news:unZlCsmhDH A.3204@TK2MSFTN GP11.phx.gbl...[color=green]
        > > try
        > > {
        > > }
        > > catch (ExceptionA A)
        > > {
        > > XXXXXXXXXXXXX
        > > }
        > > catch (ExceptionB B)
        > > {
        > > XXXXXXXXXXXXX
        > > }
        > > catch (Exception)
        > > {
        > > YYYYYYYYYYYYY
        > > }
        > >
        > > Actually the codes of hanlding both Exception A and B are same. I want[/color][/color]
        to[color=blue][color=green]
        > > put it in a single catch block. Although I know that I can group the[/color][/color]
        code[color=blue][color=green]
        > > into a function and call it in these two catch blocks, I don't want to[/color]
        > have[color=green]
        > > duplicated code. Also I don't want to give a inheritance relationship[/color][/color]
        for[color=blue][color=green]
        > > both ExceptionA and ExceptionB. I
        > >
        > > Is there anything like the following? I want to do the following because[/color]
        > if[color=green]
        > > a large number of exception use the same hanlding code, then I can group
        > > them into a small number of brackets.
        > >
        > > try
        > > {
        > > }
        > > catch (ExceptionA A)
        > > catch (ExceptionB B)
        > > {
        > > XXXXXXXXXXXXX
        > > }
        > > catch (Exception)
        > > {
        > > YYYYYYYYYYYYY
        > > }
        > >
        > > Thanks.
        > >
        > > --
        > > Franz Wong
        > > My C++ and C# Site (Traditional Chinese) : http://www.franzwong.com
        > >
        > >
        > >[/color]
        >
        >[/color]


        Comment

        Working...