C# Assign Name for Try-Catch Error
Everybody use Try-Catch in C#. I know that.
For Example;
static void Main()
{
string s = null; // For demonstration purposes.
try
{
ProcessString(s);
}
catch (Exception e)
{
Console.WriteLine("{0} Exception caught.", e);
}
}
}
Everything is开发者_StackOverflow OK.
But how can i assign name for spesific error?
For Example;
try
{
enter username;
enter E-Mail;
}
catch
{
}
- IF username is already exist,
ErrorMessage
--> ("The Username Already Exist") - IF E-Mail is already exist,
ErrorMessage
--> ("E-Mail is using")
How can i do that in C#
?
Best Regards,
Soner
if(UserNameAlreadyExists(username))
{
throw new Exception("Username already exists");
}
if(EmailAlreadyExists(email))
{
throw new Exception("Email already exists");
}
This will answer your question.
But exception handling is not to be used to perform checks like those. Exceptions are costly, and are meant for exceptional circumstances where you can't recover from the error.
When you throw exceptions you can assign messages to them:
throw new Exception("The username already exists");
But I don't think you should be throwing exceptions here, because your application is going to expect input that causes these errors; they're not exceptional conditions. Maybe you should use validators or some other kind of handler instead.
I think all these answers have a point towards the question, but if you at some point in the future want different handling per exception you'd do it as follows:
The next examples assumes you have two different exceptions implemented
try
{
if user name is exit
{
throw new UserNameExistsException("The Username Already Exist");
}
if e-mail is already exit
{
throw new EmailExistsException("The E-Mail Already Exist");
}
}
catch(UserNameExistsException ex)
{
//Username specific handling
}
catch(EmailExistsException ex)
{
//EMail specific handling
}
catch(Exception ex)
{
//All other exceptions come here!
Console.WriteLine("The Error is{0}", ex.Message);
}
try
{
if (username exists)
{
throw new Exception("The Username Already Exist");
}
if (e-mail exists)
{
throw new Exception("The E-Mail Already Exist");
}
}
catch(Exception ex)
{
Console.WriteLine("The Error is{0}", ex.Message);
}
You can do something like this:
if (UsernameAlreadyExcists(username))
{
throw new Exception("The Username Already Exist");
}
In C#, it is possible to create our own exception class. But Exception must be the ultimate base class for all exceptions in C#. So the user-defined exception classes must inherit from either Exception class or one of its standard derived classes.
using System;
class MyException : Exception
{
public MyException(string str)
{
Console.WriteLine("User defined exception");
}
}
class MyClass
{
public static void Main()
{
try
{
throw new MyException("user exception");
}
catch(Exception e)
{
Console.WriteLine("Exception caught here" + e.ToString());
}
}
}
You can throw these Exceptions and Catch them wherever in your App, although i wouldnt use Exceptions in this Case.
精彩评论