Soner Gönül
Soner Gönül

Reputation: 98858

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

But how can i assign name for spesific error?

For Example;

try
{
   enter username;
   enter E-Mail;
}
catch
{

}

How can i do that in C#?

Best Regards,

Soner

Upvotes: 3

Views: 1588

Answers (6)

qiaohua
qiaohua

Reputation: 21

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);
}

Upvotes: 2

CloudyMarble
CloudyMarble

Reputation: 37576

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.

Upvotes: 1

F.B. ten Kate
F.B. ten Kate

Reputation: 2032

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);
}

Upvotes: 2

bruno
bruno

Reputation: 1841

You can do something like this:

if (UsernameAlreadyExcists(username))
{
    throw new Exception("The Username Already Exist");
}

Upvotes: 1

Gerrie Schenck
Gerrie Schenck

Reputation: 22378

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.

Upvotes: 6

BoltClock
BoltClock

Reputation: 724392

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.

Upvotes: 3

Related Questions