如何识别错误? [英] How to identify an error?

查看:70
本文介绍了如何识别错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我希望我的应用程序执行不同的操作,具体取决于它获得的例外情况。

例如:我有一个带有唯一索引的SQL表。如果我试图

插入一条记录在其中已经过时的记录我得到了这个例外:无法插入

对象''tblTelephones'中的重复键行具有独特的索引

''UniqueValues''。"


我正在寻找的是一种识别异常的方法:万一我得到这个

例外我想这样做,如果是另一个我想这样做。


对我来说最简单的解决方案似乎是:

Catch ex As Exception

如果离开(ex.Message,41)<> 无法在

对象中插入重复的键行然后

''做这个动作

结束如果


Q虽然,我不相信这是最好的方法。有没有办法用
来识别具有唯一编号的例外?或者我曾经看过一些

就像错误的名字一样。有人可以帮我这个吗?


非常感谢提前


彼得

解决方案

解决这个问题的一种方法是将列种子设置为-1并倒计时。这种方式

插入新记录时,如果你的

记录都是正数,你将不会重复。


但是,我建议如果可能的话你抛弃这个想法并使用

GUID',这是非常可靠的,因为这是一个独特性(不过完全不是
)。 />

问候 - OHM


" DraguVaso" < PI ********** @ hotmail.com>在消息中写道

新闻:Ol ************** @ TK2MSFTNGP10.phx.gbl ...


我希望我的应用程序根据它获得的异常执行不同的操作。
例如:我有一个带有唯一索引的SQL表。如果我试图在其中插入一条已经过时的记录,我会得到以下异常:无法在具有唯一索引的对象''tblTelephones'中插入
重复键行
' 'UniqueValues''。"

我正在寻找的是一种识别异常的方法:如果我得到
这个例外,我想这样做,如果是另一个我想这样做。

对我来说最简单的解决方案似乎是:
Catch ex As Exception
如果Left(ex.Message,41)<> 无法在
对象中插入重复的键行然后
''做这个动作
结束如果

Q虽然,我不相信这是最好的方式。有没有办法用唯一的数字来识别异常?或者我曾经看过一些像错误名称的东西。有人可以帮我这个吗?

非常感谢提前

Pieter



< blockquote>

如果是SQL错误,你可能会捕获System.Data.SqlClient.SqlException而不是
of Exception。

在SqlException中,你有几个属性,如数字,给你

你的具体错误。


所以在客户端你可能想做这样的事情:

catch(SqlException exp)

{

if(exp.Number == 1801)

做行动;

else if(exp.Number == 1802)

do action2;

else WriteErrorMessage("无法执行此操作,SQL错误desription" +

exp.Message);

}

catch(Exception exp)

{

WriteErrorMessage(无法执行此操作,错误描述+

exp.Message);

}


您也可以定义您的摘要并执行以下操作服务器端:

catch(SqlException exp)

{

if(exp.Number == 1801)

抛出新的MyException1();

else if(exp.Number == 1802)

抛出新的MyException2();

else throw;

}


在客户端捕获这些异常并执行所需的操作。


它不是一个好主意是在消息字符串上加上if语句。


" DraguVaso" < PI ********** @ hotmail.com>在消息中写道

新闻:Ol ************** @ TK2MSFTNGP10.phx.gbl ...


我希望我的应用程序根据它获得的异常执行不同的操作。
例如:我有一个带有唯一索引的SQL表。如果我试图在其中插入一条已经过时的记录,我会得到以下异常:无法在具有唯一索引的对象''tblTelephones'中插入
重复键行
' 'UniqueValues''。"

我正在寻找的是一种识别异常的方法:如果我得到
这个例外,我想这样做,如果是另一个我想这样做。

对我来说最简单的解决方案似乎是:
Catch ex As Exception
如果Left(ex.Message,41)<> 无法在
对象中插入重复的键行然后
''做这个动作
结束如果

Q虽然,我不相信这是最好的方式。有没有办法用唯一的数字来识别异常?或者我曾经看过一些像错误名称的东西。有人可以帮我这个吗?

非常感谢提前

Pieter



< blockquote>谢谢Tal!这正是我所需要的!


" Tal" < TA ** @ nice.com>在消息中写道

news:ed ************** @ tk2msftngp13.phx.gbl ...

如果是SQL错误,你可能会捕获System.Data.SqlClient.SqlException
而不是异常。
在SqlException中,你有几个属性,比如
给你特定的错误。

所以在客户端你可能想做类似的事情:
catch(SqlException exp)
{
if(exp.Number == 1801)
做动作;
如果(exp.Number == 1802)
做动作2;
否则WriteErrorMessage(无法做这个操作,SQL错误描述+
exp.Message);
}
catch(Exception exp)
{WriteErrorMessage(无法执行此操作,错误描述+
exp。消息);
}

您也可以定义您的激活并在服务器
中执行以下操作:catch(SqlException exp)
{
if (exp.Number == 1801)
thro w new MyException1();
else if(exp.Number == 1802)
抛出新的MyException2();
抛出;
}

并且在客户端捕获这些异常并执行所需的操作。

在消息字符串上使用if语句不是一个好主意。

" DraguVaso" < PI ********** @ hotmail.com>在消息中写道
新闻:Ol ************** @ TK2MSFTNGP10.phx.gbl ...


我希望我的应用程序根据它获得的异常执行不同的操作。
例如:我有一个带有唯一索引的SQL表。如果我试图在其中插入一条非常好的记录,我会得到以下异常:不能
在具有唯一索引的对象''tblTelephones'中插入重复的键行
' 'UniqueValues''。

我正在寻找的是一种识别异常的方法:如果我得到


这个

例外我想这样做,如果是另一个我想这样做。

对我来说最简单的解决方案似乎是:
Catch ex As Exception
如果离开( ex.Message,41)<> 无法在对象中插入重复的键行
然后
''做这个动作
结束如果

Q虽然,我不相信这是最好的方式。有没有办法用唯一的数字来识别异常?或者我曾经看过一些像错误名称的东西。有人可以帮我这个吗?

非常感谢提前

Pieter




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

解决方案

One way around this is to set the column seed to -1 and count down. This way
when the new records are inserted you will not get duplicates assuming your
records are all positive.

However, I would recommend that if possible you dump this idea and use
GUID''s which are extremely reliable as far a uniqueness is concerned ( not
totally though ).

Regards - OHM

"DraguVaso" <pi**********@hotmail.com> wrote in message
news:Ol**************@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



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" <pi**********@hotmail.com> wrote in message
news:Ol**************@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



Thanks Tal! That was exactly what I needed!

"Tal" <ta**@nice.com> wrote in message
news:ed**************@tk2msftngp13.phx.gbl...

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" <pi**********@hotmail.com> wrote in message
news:Ol**************@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




这篇关于如何识别错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆