Re: How to identify an error?

From: Tal (talg_at_nice.com)
Date: 06/10/04


Date: Thu, 10 Jun 2004 11:49:58 +0300

Hi,
If it is SQL error, you may catch System.Data.SqlClient.SqlException instead
of Exception.
In SqlException, you are having several properties such as Number that give
you the specific error.

So in the client side you may want to do something like that:
catch (SqlException exp)
{
    if (exp.Number == 1801)
        do action;
    else if (exp.Number == 1802)
        do action2;
    else WriteErrorMessage ("Fail to do this op, SQL error desription" +
exp.Message);
}
catch (Exception exp)
{
    WriteErrorMessage ("Fail to do this op, error desription" +
exp.Message);
}

You may also define your excpetions and do the following in the server side:
catch (SqlException exp)
{
    if (exp.Number == 1801)
        throw new MyException1();
    else if (exp.Number == 1802)
        throw new MyException2();
    else throw;
}

And in the client side catch those exceptions and do the needed actions.

It is not a good idea to have if statement on the message string.

"DraguVaso" <pietercoucke@hotmail.com> wrote in message
news:OlPwtMsTEHA.2464@TK2MSFTNGP10.phx.gbl...
> Hi,
>
> I want my application do different actions depending on the exception it
> gets.
> For exemple: I have an SQL-table with a unique index. In case I try to
> Insert a record that's alreaddy in it I get this exception: "Cannot insert
> duplicate key row in object 'tblTelephones' with unique index
> 'UniqueValues'."
>
> What I'm looking for is a way to identify the exception: in case I get
this
> exception I want to do this, in case of another I want to do that.
>
> The most simple solution to me seems this:
> Catch ex As Exception
> If Left(ex.Message, 41) <> "Cannot insert duplicate key row in
> object" Then
> 'do this action
> End If
>
> QAlthough, I'm not convinced this is the best way. Is there a way to
> identify the exception with a unique number? Or I've seen once something
> like a name for an error. Can anybody help me with this?
>
> Thanks a lot in advance
>
> Pieter
>
>
>



Relevant Pages

  • Re: SqlException deserialization
    ... InitialCatalog are invalid so a login exception is thrown. ... The stacktrace before serialisation is: ... > without deserialization). ... >> soapformatter and then deserialize and the resulting SqlException has ...
    (microsoft.public.dotnet.framework.adonet)
  • Sqlexception serialization/deserialization
    ... I'm having a problem with a SqlException that I am serializing and ... is a sql exception with the following message: ... StackTrace property. ... soapformatter and then deserialize and the resulting SqlException has ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: wtf?
    ... > called by the ASP classic app, ... >> other than error translation. ... > exception that comes from the command's execute method doesn't seem to ... > the time we might be content saying "SqlException", in this case, we ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: a criticism of java
    ... > a)Integer.parseInt was one of the method that got up my nose. ... catch (final Exception exception) ... IOException and a NumberFormatException with it, ... SQLException, and just logging it. ...
    (comp.lang.java.programmer)
  • Re: What SqlExceptions can be thrown
    ... There is no case where a SqlException exception would be ... > If all you need to know is if the stored procedure you ran completed ... > have more information based on the specific errors that SQL server returns. ...
    (microsoft.public.dotnet.languages.csharp)