C# 使用try/catch使我可以';t在try/catch块之外使用变量
我有一个从web服务获取C# 使用try/catch使我可以';t在try/catch块之外使用变量,c#,try-catch,scope,C#,Try Catch,Scope,我有一个从web服务获取产品对象的代码。如果没有产品,它将返回一个EntityDoesNotExist异常。我需要处理这件事。。但是,我有很多其他代码处理返回的产品,但是如果该代码不在try/catch中,它就不起作用,因为产品基本上没有定义。在try/catch中包含我的其他相关代码是实现此功能的唯一方法吗?这看起来真的很草率 代码示例: try { Product product = catalogContext.GetProduct("CatalogName", "ProductI
产品
对象的代码。如果没有产品,它将返回一个EntityDoesNotExist
异常。我需要处理这件事。。但是,我有很多其他代码处理返回的产品
,但是如果该代码不在try/catch中,它就不起作用,因为产品
基本上没有定义。在try/catch中包含我的其他相关代码是实现此功能的唯一方法吗?这看起来真的很草率
代码示例:
try {
Product product = catalogContext.GetProduct("CatalogName", "ProductId");
} catch(EntityDoesNotExist e) {
// Do something here
}
if(dataGridView1.InvokeRequired) {
// Do something in another thread with product
}
只有包含我的其他相关代码才能使此工作正常
在试一试中
否。即使如果Web服务未返回产品
,则会引发EntityDoesNotExist
异常,您也需要在try块外部声明本地产品
变量,以便try块外部的相关代码可以访问它
在try{}catch{}
之外声明产品
:
Product product = null;
try
{
product = catalogContext.GetProduct("CatalogName", "ProductId");
}
catch(EntityDoesNotExist e)
{
// Do something here
}
if(dataGridView1.InvokeRequired)
{
// Do something in another thread with product
}
只有包含我的其他相关代码才能使此工作正常
在试一试中
否。即使如果Web服务未返回产品
,则会引发EntityDoesNotExist
异常,您也需要在try块外部声明本地产品
变量,以便try块外部的相关代码可以访问它
在try{}catch{}
之外声明产品
:
Product product = null;
try
{
product = catalogContext.GetProduct("CatalogName", "ProductId");
}
catch(EntityDoesNotExist e)
{
// Do something here
}
if(dataGridView1.InvokeRequired)
{
// Do something in another thread with product
}
只需在try/catch范围之外声明它
Product product;
try
{
product = catalogContext.GetProduct("CatalogName", "ProductId");
}
catch (EntityDoesNotExist e)
{
product = null;
}
if (dataGridView1.InvokeRequired)
{
// use product here
}
只需在try/catch范围之外声明它
Product product;
try
{
product = catalogContext.GetProduct("CatalogName", "ProductId");
}
catch (EntityDoesNotExist e)
{
product = null;
}
if (dataGridView1.InvokeRequired)
{
// use product here
}
如果在获取产品时引发了异常,那么您就没有可操作的产品。似乎应该确保只有在没有引发异常的情况下才执行UI代码。这可以通过将该代码移动到
try
块中来实现:
try
{
Product product = catalogContext.GetProduct("CatalogName", "ProductId");
if (dataGridView1.InvokeRequired)
{
// Do something in another thread with product
}
}
catch (EntityDoesNotExist e)
{
// Do something here
}
如果在获取产品时引发了异常,那么您就没有可操作的产品。似乎应该确保只有在没有引发异常的情况下才执行UI代码。这可以通过将该代码移动到
try
块中来实现:
try
{
Product product = catalogContext.GetProduct("CatalogName", "ProductId");
if (dataGridView1.InvokeRequired)
{
// Do something in another thread with product
}
}
catch (EntityDoesNotExist e)
{
// Do something here
}
如果在try-catch中声明,如何访问try-catch外部的
产品
?如果在try-catch中声明,如何访问try-catch外部的产品
?使用此选项,之后我尝试执行MessageBox.Show(p.SomeProperty)
它说:使用未分配的局部变量'p'@stephennoth您必须在try
之前分配product
一些值(例如product product=null;
),或者在catch
中(如此解决方案所示),或者让catch
始终再次抛出,不仅要在中分配它,还要尝试。try
中的内容被视为可能会运行,而不是将要运行,因此在该上下文中,p
可能会被取消分配。@TimS。是的,就是这样。谢谢。使用这个,之后我试着做MessageBox.Show(p.SomeProperty)
,它说:使用未赋值的局部变量'p'@stephennoth,您必须在try
之前(例如product-product=null;
)或catch
(如此解决方案所示)分配product
一些值,或者让catch
始终再次抛出,而不仅仅是在try
中分配它。try
中的内容被视为可能会运行,而不是将要运行,因此在该上下文中,p
可能会被取消分配。@TimS。是的,就是这样。谢谢