目前正在开发一个小项目,其中包括一个注册表,用户可以在其中注册登录。我有错误提供程序,告诉用户他们输入的信息是否不正确。由于某种原因,不再处理例外情况,即使它们已经有一段时间了。
以下是用户按下“注册按钮”
时的代码private void btnRegister_Click(object sender, EventArgs e)
{
//Try - Catch Statements
//Try to set the forename
try
{
player1.Forename = txtForename.Text;
player2.Forename = txtForename.Text;
errForename.Icon = Properties.Resources.Correct;
errForename.SetError(txtForename, "OK");
}
catch (NewException exc)
{
errForename.SetError(txtForename, exc.MessageM);
}
然后执行此操作以获取其他详细信息,例如姓氏,用户名。
这是User类的getter和setter
public string Forename
{
get
{
return forename;
}
set
{
bool okChar = OnlyChars(value, "Forename");
if (okChar == true)
forename = value;
else
{
throw new NewException(errorMessage);
forename = null;
}
}
}
最后这是我写的NewException类
namespace Colludia
{
class NewException : Exception
{
private string messageM;
public NewException() : base()
{
}
public NewException(string message) : base(message)
{
messageM = message;
}
public string MessageM
{
get { return messageM; }
set { messageM = value; }
}
}
}
答案 0 :(得分:0)
这是适合你的代码。 我认为它的名称空间问题
using System;
class Player
{
string forename = "";
string errorMessage = "";
public string Forename
{
get
{
return forename;
}
set
{
bool okChar = OnlyChars(value, "Forename");
if (okChar == true)
forename = value;
else
{
throw new NewException(errorMessage);
}
}
}
private bool OnlyChars(string value, string p)
{
if (value == p)
return true;
else
{
errorMessage = "Err";
return false;
}
}
}
class NewException : Exception
{
private string messageM;
public NewException()
: base()
{
}
public NewException(string message)
: base(message)
{
messageM = message;
}
public string MessageM
{
get { return messageM; }
set { messageM = value; }
}
}
class Program
{
static void Main(string[] args)
{
try
{
var player1 = new Player();
var player2 = new Player();
player1.Forename = "asdfg";
player2.Forename = "qwerty";
}
catch (NewException exc)
{
bool error = true;
}
}
}