抛出自定义的异常错误 [英] Throwing customised exception errors

查看:124
本文介绍了抛出自定义的异常错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了一个代码向用户抛出错误,但在后端显示错误以及代码。



这就是它显示的内容对于用户,但我只需要向他展示messgae;





Hi,i have used a code to throw error to a user but shows the error in back end together with the code,.

This is what it shows to a user but i need to show him the messgae only;


Email Address Exists in Records
<pre lang="c#">
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.







异常详情:System.Exception:记录中存在电子邮件地址



来源错误:





第231行:

第232行:

第233行:抛出前;

第234行://返回;

235行:



这是我在代码中使用的行



抛出新的异常(已经存在电子邮件);





他们是一个以友好的方式抛出此错误的好方法?




Exception Details: System.Exception: Email Address Exists in Records

Source Error:


Line 231:
Line 232:
Line 233: throw ex;
Line 234: //return ;
Line 235:

This is the line i have used in my code

throw new Exception("Email already exists");


Is their a good way to throw this error in a friendly way?

推荐答案

就我个人而言,我不喜欢为不例外的事情抛出异常,例如检查电子邮件并且它已经存在。按照自己的方式行事没有任何问题,这将有效。然后你需要抓住它或者有一个通用捕手,它至少会以友好的方式显示给用户。



对于检查电子邮件是否已经存在的事情你应检查,如果存在则返回一个值,然后在屏幕上正确显示。我建议不要抛出异常。
Personally I don't like throwing exceptions for things that are not exceptional, such as checking for an email and it already exists. There is nothing wrong with doing it your way, that will work. You then need to catch it or have a generic catcher that will at least display it to the user in a friendly way.

For something like checking if email already exists you should check, and if it exists return a value and then display on screen appropriately. I would recommend against throwing an exception.


这不是一种正确的方法。您应该检查该值的存在(电子邮件)&然后向用户显示消息而不是抛出异常。下面的样本

为此写一个函数

That's not a right way to do that. You should check the existence of that value(email) & then show message to user instead of throwing exception. Sample one below
Write a function for that
function bool IsEmailExists(string strEmail)
{
   //Some code to check existense of Email
   //like a query "SELECT * FROM Table WHERE Email = @Email"
}



在执行操作之前使用该功能


Use that function before doing your operation

sub SomeMethod()
{
  //Some massive code
  if(IsEmailExists(txtEmail.Text))
  {
    lblError.Text = "This email already exists, choose different one";
    return;
  }
  else
  {
    //Do some operation based on email like Registration for entered Email
  }
  //Some massive code
}



点是不要总是抛出异常,必要时使用 。



要了解例外,请查看这些文章

.NET中的异常处理最佳实践 [ ^ ]

C#初学者的异常处理 [ ^ ]


您使用的是ASP.NET应用程序吗?



创建一个空起始值的标签并给出一个名称。所以,在你的catch语句中设置如下:



Do you use ASP.NET Application?

Create a label with a empty start value and give a name. so, into you catch statement set like this:

try{}catch(Exception e) 
{  
   lblErro.ForeColor = Color.Red;
   lblErro.Text = "*** ERRO: " + ex.Message;
}





如果您的方法不在您的图层视图中,请使用您的消息抛出异常,并使用我的代码。

所以你将成为你的例外。 aspx文件。



if your method is not in your layer view, throws your exception with you message, and use my code.
so you shall be your exception to. aspx file.


这篇关于抛出自定义的异常错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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