Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用try/catch使我可以';t在try/catch块之外使用变量_C#_Try Catch_Scope - Fatal编程技术网

C# 使用try/catch使我可以';t在try/catch块之外使用变量

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

我有一个从web服务获取
产品
对象的代码。如果没有产品,它将返回一个
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。是的,就是这样。谢谢