C# - 如何在 try catch 块中使用未分配的变量
crmFactory.RegisterDemoAccount
抛出 Exception
。为了使用变量 res,我需要初始化它。
由于 AccountRegistrationResponse
不可初始化,如何声明 res
而不会出现有关使用未分配变量的编译错误? 我可以将其分配为 null,但我认为这不是一个好的编程方法。
AccountRegistrationResponse res /*=null*/;
try
{
res = crmFactory.RegisterDemoAccount(CrmConfigRepository.CrmOwnerUserId
, CrmConfigRepository.CrmOrganizationName
, CrmConfigRepository.CrmBusinessUnitName
, demo.getData());
}
catch (Exception e)
{
_log.Error("Cannot create demo account", e);
}
_log.Debug(res.getString());
crmFactory.RegisterDemoAccount
throws Exception
. In order to use the variable res
I need to initialize it.
Since AccountRegistrationResponse
is not initializable, how can I declare res
without getting compilation errors about using unassigned variables?
I can assign it to null, but I don't think this is a good programming approach.
AccountRegistrationResponse res /*=null*/;
try
{
res = crmFactory.RegisterDemoAccount(CrmConfigRepository.CrmOwnerUserId
, CrmConfigRepository.CrmOrganizationName
, CrmConfigRepository.CrmBusinessUnitName
, demo.getData());
}
catch (Exception e)
{
_log.Error("Cannot create demo account", e);
}
_log.Debug(res.getString());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您不应该在捕获未知异常后尝试继续您的方法。任何事情都可能出错,假设继续安全是没有意义的。如果你尝试的话,只会发生不好的事情。
要么返回错误结果,要么更好,只是重新抛出原始异常:
现在编译器可以看到,在 try 块成功完成后,
res
始终会被赋值。You shouldn't try to continue your method after catching an unknown exception. Anything could have gone wrong and it makes no sense to assume that it's safe to continue. Only bad things can happen if you try.
Either return an error result, or better, just rethrow the original exception:
Now the compiler can see that
res
will always be assigned after the try block completes successfully.我理解您不愿意将
res
分配给 null - 这感觉毫无意义,因此是错误的。不过,当在分配对象的块之外需要对象时,这是这种情况下的常见方法。假设您在 try/catch 块中分配变量时做了正确的事情(在许多情况下,这不是一种不常见的模式),我不会担心它。但是,如果任务失败了怎么办?第二个日志记录调用将尝试取消引用
res
,并抛出NullReferenceException
。那不好。I understand your reluctance to assign
res
to null - it feels pointless, and therefore wrong. It is a common approach in situations like this, though, when an object is needed outside the block in which it's assigned. Assuming you're doing the right thing in assigning your variable in a try/catch block (and it's not an uncommon pattern, in many cases), I wouldn't worry about it.However, what would happen if the assignment failed? The second logging call would try to dereference
res
, and throw aNullReferenceException
. That's not good.您需要将日志行放在 try/catch 中,以便编译器知道
res
已初始化。You need to put the logging line inside the try/catch so that the compiler knows that
res
has been initialised.这是正确的方法。唯一的事情是,如果
null
是RegisterDemoAccount
的有效返回值,您可以添加一个设置为true 的
就在boolinitialized = false
RegisterDemoAccount
之后。It's THE right approach. Only thing, if
null
is a valid return value ofRegisterDemoAccount
, you could add abool initialized = false
that you set totrue
just after theRegisterDemoAccount
.正如您所说,如果您在 try/catch 之外需要它,请将其分配为 null。这不是一个坏的编程方式。
Assign it to null, like you said, if you need it outside of try/catch. It's not bad way of programming.
为什么?如果您没有初始化
res
,然后RegisterDemoAccount(...)
(或其之前的另一个表达式)抛出异常,则res
将不会被分配在try
语句中。因此,执行可能会在
res
未分配的情况下到达最终语句(在catch
块之后)。问题是在最后一个语句中使用了 res - 编译器可以看到它可以在没有初始化的情况下达到这一点。
Why? If you don't initialise
res
and thenRegisterDemoAccount(...)
(or another expression before it) throws thenres
will not be assigned in thetry
statement.Therefore execution could reach the final statement (after the
catch
block) withres
unassigned.The problem is the use of
res
in that last statement – the compiler can see it can get to this point without initialisation.