抛出异常问题 [英] Throwing exceptions problem

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

问题描述




我知道throw语句会立即抛出异常并且无条件地抛出
。控制永远不会在抛出后的
之后立即到达声明。以下方法似乎在抛出后停止语句(如果

Enum.IsDefined为false)并将null返回到调用的

继续。然后调用proc引发NullReferenceException。基本上

它在我看来就像throw语句没有立即停止程序

。这可能是真的还是我错过了什么?

public int AddAccount(... byte accountType ...)

{

//检查有效的帐户类型是否存在为AccountType Enum

if(!Enum.IsDefined(typeof(AccountTypes),accountType))

抛出新的ArgumentOutOfRangeException(" AccountType must匹配

枚举。);


....

//做一些数据库插入

//添加新帐户

返回帐户。帐户ID;

}


非常感谢

Andrew

Hi

I understand that a throw statement throws an exception immediately and
unconditionally. Control never reaches the statement immediately following
the throw. The following method seems to stop the statements (if
Enum.IsDefined is false) after the throw and return null to the calling
proceedure. Then the calling proc raises a NullReferenceException. Basically
it looks to me like the throw statement is not stopping the programme
immediately. Can this be true or have I missed something?
public int AddAccount(...byte accountType...)
{
// Check that a valid account type exists as an AccountType Enum
if (!Enum.IsDefined(typeof(AccountTypes), accountType))
throw new ArgumentOutOfRangeException("AccountType must have a matching
Enum.");

....
// do some db inserting
// Add the new account
return account.AccountID;
}

Many thanks
Andrew

推荐答案

" J055" < j0 ** @newsgroups.nospamwrote:
"J055" <j0**@newsgroups.nospamwrote:

我知道throw语句会立即抛出异常,并且无条件地抛出
。控制永远不会在抛出后的
之后立即到达声明。
I understand that a throw statement throws an exception immediately and
unconditionally. Control never reaches the statement immediately following
the throw.



是。

Yes.


以下方法似乎停止了陈述(如果

Enum.IsDefined为false)抛出后返回null
The following method seems to stop the statements (if
Enum.IsDefined is false) after the throw and return null



这是不可能的,因为该方法返回一个''int''值,其中

不能为空。

That is not possible, since the method returns an ''int'' value, which
cannot be null.


来电话

继续。然后调用proc引发NullReferenceException。基本上

它在我看来就像throw语句没有立即停止程序

。这可能是真的还是我错过了什么?
to the calling
proceedure. Then the calling proc raises a NullReferenceException. Basically
it looks to me like the throw statement is not stopping the programme
immediately. Can this be true or have I missed something?


public int AddAccount(... byte accountType ...)

{
public int AddAccount(...byte accountType...)
{



代码看起来应该抛出异常或返回帐户的

值.AccountID - 如果你得到NullReferenceException

可能是因为''account''为空。如果没有更多信息或更完整的代码,就不可能多说多少




- Barry


-
http://barrkel.blogspot.com/


您好Barry


这是完整的方法。我的枚举不包含0值,因此如果我在帐号类型0中发送

,则应引发我的ArgumentOutOfRangeException。我知道如果我从方法中删除throw语句那么它总是

返回一个整数。所以我不明白为什么它应该返回null如果

ArgumentOutOfRangeException被引发。


谢谢

Andrew


public int AddAccount(字符串代码,


字符串密码,

string friendlyName,


string firstName,


string lastName,


byte accountType,

bool isActive,

字节角色





{


//检查有效的帐户类型是否存在为帐户类型枚举


如果(!Enum.IsDefined(typeof(AccountTypes),accountType))


抛出新的ArgumentOutOfRangeException(AccountType必须匹配

Enum。) ;


//检查AccountRoles枚举中是否存在有效角色


if(!Enum.IsDefined(typeof(AccountRoles),角色) ))


抛出新的ArgumentOutOfRangeException(&qu ot;角色必须匹配

AccountRoles枚举。);

//创建一个新的AccountsRow实例


ScsDataSet .AccountsDataTable accounts = new ScsDataSet.AccountsDataTable();

ScsDataSet.AccountsRow account = accounts.NewAccountsRow();


account.Code =代码;


account.Email =电子邮件;


account.Password =密码;


account.FriendlyName = friendlyName;


account.FirstName = firstName;


account.LastName = lastName;


account.AccountType = accountType;


account.IsActive = isActive;


account.Role = role;


//添加新帐户


accounts.AddAccountsRow(account);


int rowsAffected = Adapter。更新(帐户);

返回帐户。帐户ID;


}
Hi Barry

This is the complete method. My Enums don''t contain a 0 value so if I send
in an accountType of 0 my ArgumentOutOfRangeException should be raised. I
know that if I remove the throw statements from the method then it always
return an integer. So I don''t understand why it should return null if the
ArgumentOutOfRangeException is raised.

Thanks
Andrew

public int AddAccount(string code,

string email,

string password,

string friendlyName,

string firstName,

string lastName,

byte accountType,

bool isActive,

byte role

)

{

// Check that a valid account type exists as an AccountType Enum

if (!Enum.IsDefined(typeof(AccountTypes), accountType))

throw new ArgumentOutOfRangeException("AccountType must have a matching
Enum.");

// Check that a valid role exists in the AccountRoles Enum

if (!Enum.IsDefined(typeof(AccountRoles), role))

throw new ArgumentOutOfRangeException("Role must have a matching
AccountRoles Enum.");
// Create a new AccountsRow instance

ScsDataSet.AccountsDataTable accounts = new ScsDataSet.AccountsDataTable();

ScsDataSet.AccountsRow account = accounts.NewAccountsRow();

account.Code = code;

account.Email = email;

account.Password = password;

account.FriendlyName = friendlyName;

account.FirstName = firstName;

account.LastName = lastName;

account.AccountType = accountType;

account.IsActive = isActive;

account.Role = role;

// Add the new account

accounts.AddAccountsRow(account);

int rowsAffected = Adapter.Update(accounts);
return account.AccountID;

}


嗨再次


不知道是不是很有帮助。 ObjectDataSource.InsertMethod设置为

AddAccount方法。然后我将Inserted事件处理返回的

整数。这是我得到NullReferenceException的地方,因为

e.ReturnValue是一个空对象。为什么应用程序不会停止在我的

抛出异常?它似乎迷路了,即没有堆栈跟踪信息或

innerexception似乎可用。


protected void odsAccount_Inserted(object sender,

ObjectDataSourceStatusEventArgs e)

{

Trace.Warn(e.Exception.Message);

//写出:异常已被删除由调用目标抛出。


//将新的AccountID添加到选择参数

odsAccount.SelectParameters.Add(" AccountID",TypeCode) .Int32,

e.ReturnValue.ToString());

}
Hi again

Don''t know if this helps. The ObjectDataSource.InsertMethod is set to the
AddAccount method. I then have the Inserted event handle the returned
integer. This is where I get a NullReferenceException becuase the
e.ReturnValue is a null object. Why does the application not stop at my
thrown exception? It seems to get lost, i.e. no stack trace information or
innerexception seems to be available.

protected void odsAccount_Inserted(object sender,
ObjectDataSourceStatusEventArgs e)
{
Trace.Warn(e.Exception.Message);
// writes out: Exception has been thrown by the target of an invocation.

// add the new AccountID to the Select Parameters
odsAccount.SelectParameters.Add("AccountID", TypeCode.Int32,
e.ReturnValue.ToString());
}


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

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