Click here to Skip to main content
15,878,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;


CSS
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.



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?
Posted
Updated 17-Jan-14 2:28am
v3

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.
 
Share this answer
 
Comments
Ron Beyer 17-Jan-14 10:33am    
+5, exceptions are things that the current set of code can't handle so it tells the application to pass it up until something can handle it. If nothing does, it presents it to the user. I also would not use an exception where you can handle it easily in a more friendly way.
thatraja 17-Jan-14 10:42am    
Beat me to it. 5!
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
C#
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
C#
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
}

Point is "Don't throw exceptions always, use when necessary".

To learn about Exceptions, check these articles
Exception Handling Best Practices in .NET[^]
Exception Handling for C# Beginners[^]
 
Share this answer
 
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:

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


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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900