Exception identifier

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

    Exception identifier

    Hi all, I have a question about capturing exception details in C#.

    Is it possible to query the exception and get an id related to what caused
    the exception??

    e.g

    1 = Tried to enter a row into a database but there was a Primary Key
    conflict

    Currently I am doing something like this

    catch(Exception ex){
    if (ex.Message.ToS tring().ToLower ().IndexOf("pri mary key")>0){

    But this looks decidely suspect so if anyone knows of a better way, I am all
    ears ;)

    Thanks
    Mark





  • MuZZy

    #2
    Re: Exception identifier

    John wrote:[color=blue]
    > Hi all, I have a question about capturing exception details in C#.
    >
    > Is it possible to query the exception and get an id related to what caused
    > the exception??
    >
    > e.g
    >
    > 1 = Tried to enter a row into a database but there was a Primary Key
    > conflict
    >
    > Currently I am doing something like this
    >
    > catch(Exception ex){
    > if (ex.Message.ToS tring().ToLower ().IndexOf("pri mary key")>0){
    >
    > But this looks decidely suspect so if anyone knows of a better way, I am all
    > ears ;)
    >
    > Thanks
    > Mark
    >[/color]

    Define your own exception class derived from Exception like this:
    class MyException: Exception
    {
    int ErrCode;
    MyException(int err, string msg): base(msg)
    {
    ErrCode = err;
    }
    }

    Something like that. And when you catch the exception, you know the err code and explanation.

    catch (Exception ex)
    {
    if (ex is MyException)
    if (((MyExcption)e x).ErrCode = 5)
    {...do whatever...}
    }

    Hope it helps
    Andrey

    Comment

    • Mattias Sjögren

      #3
      Re: Exception identifier

      [color=blue]
      >Is it possible to query the exception and get an id related to what caused
      >the exception??[/color]

      If its a SqlException you're catching, you can get plenty more
      information from the Errors property (a collection of SqlError, which
      have a numeric Number property).




      Mattias

      --
      Mattias Sjögren [MVP] mattias @ mvps.org
      http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
      Please reply only to the newsgroup.

      Comment

      • John

        #4
        Re: Exception identifier

        Thanks, I'll take a look at these ideas
        Regards
        Mark
        "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
        news:eYlW3xTFFH A.1396@tk2msftn gp13.phx.gbl...[color=blue]
        >[color=green]
        > >Is it possible to query the exception and get an id related to what[/color][/color]
        caused[color=blue][color=green]
        > >the exception??[/color]
        >
        > If its a SqlException you're catching, you can get plenty more
        > information from the Errors property (a collection of SqlError, which
        > have a numeric Number property).
        >
        >
        >
        >
        > Mattias
        >
        > --
        > Mattias Sjögren [MVP] mattias @ mvps.org
        > http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
        > Please reply only to the newsgroup.[/color]


        Comment

        Working...