识别自定义引发的错误 [英] Identify custom thrown errors
问题描述
我在sql中整理自定义错误,就像这样
I''m razing custom errors in sql, like this
RAISERROR( ''Invalid id'', 16, 1 )
RETURN
我想向用户显示此(自定义引发)错误,但不向其他任何SQL示例显示.
例如
I want to show this(custom raised) error to user but not any other sql exeptions.
for example
try
{
}
catch(SqlException ex)
{
if (customRaisedError) //how to know ?
{
//show
}
else
{
//dont show
}
}
catch(Exception ex)
{
//show all these error
}
我可以在消息中使用前缀,但是我不喜欢这种方式.你们知道其他完美的方法吗?
I can use prefix in message, but I do not like this way. Any other perfect way you guys know ?
推荐答案
我希望这会对您有所帮助
SqlException转换为自定义异常处理程序 [
I hope this will help you
SqlException conversion to a custom Exception handler[^]
是的,每当您在SQL中引发此自定义错误时,您都可以返回一些整数值,例如
Yes, whenever you get to raise this custom error in SQL, you can return some integer value like
RAISERROR( 'Invalid id', 16, 1 )
SELECT '1';
RETURN
这样,您可以在执行存储过程时在前端获取任何引发的错误的单独值,并且可以在前端相应地显示消息,然后将其空白放置为
This way you can get a separate value for any raised error at the front-end while executing the stored procedure, and you can display the message accordingly on your front end, and then put a blank as
return "";
在其他catch块中,以避免向用户显示这些异常.
希望对您有所帮助.
in other catch blocks, to avoid showing those exceptions to users.
Hope this helps.
您好XMen
您可以创建自定义的异常处理程序.
在sql中为错误代码/描述/语言列创建查找表并启用.
如果启用的是位0(关闭),则可以在代码区域中引发默认异常.
否则将引发与表条目相关的异常.
我建议您在初始化启动过程中,在应用程序中填充查找内容.然后更容易将其从错误类或组件/管理器中抛出
Hello XMen
You can create a custom Exception Handler.
Create a lookup table in sql for the error code / description / language columns and enabled.
if the enabled is bit 0 (off) you can throw the default exception in your code area.
Otherwise throw the exception that related to the table entry.
i advise you populate the content of the lookup in your app as part of the initialization starting process. then easier to throw it from a error class or component / manager
int y = 0;
try
{
y = 2* 8888888888888888888888888888888 ^ 999999999999999999999
}
catch (Exception _Ex)
{
DisplayCustomErrror(_Ex);
}
finally
{
y = 2;
}
public void DisplayCustomErrror(Exception _Ex)
{
// Check if the code error exists in your list of errors.
// if not amend to it to the table as a new entry with a default of the standard exception message - you can get change it after wards when the app is distributed.
// Throw Custom Exception
// If custom Exception doesn't exist - throw default
}
这篇关于识别自定义引发的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!