Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.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# 在if-else块中引发异常_C#_Exception Handling_C# 3.0 - Fatal编程技术网

C# 在if-else块中引发异常

C# 在if-else块中引发异常,c#,exception-handling,c#-3.0,C#,Exception Handling,C# 3.0,首先,我意识到可能有更好的方法来实现这一点,与其抛出异常,我应该更好地处理这种情况。话虽如此,我遇到了一些意想不到的行为,我更好奇为什么会发生这种情况,而不是将其用于我的应用程序 在方法中,我试图访问用户提供的文件。在方法的开头,我检查以确保文件的路径不是Null或String.Empty,如果是,则抛出异常。当我进行测试时,我发现不管条件如何,都会抛出异常。这是正常的行为,还是我遗漏了什么 public static XElement Foo(String path) { if (St

首先,我意识到可能有更好的方法来实现这一点,与其抛出异常,我应该更好地处理这种情况。话虽如此,我遇到了一些意想不到的行为,我更好奇为什么会发生这种情况,而不是将其用于我的应用程序

在方法中,我试图访问用户提供的文件。在方法的开头,我检查以确保文件的路径不是
Null
String.Empty
,如果是,则抛出异常。当我进行测试时,我发现不管条件如何,都会抛出异常。这是正常的行为,还是我遗漏了什么

public static XElement Foo(String path)
{
    if (String.IsNullOrEmpty(path))
    {
       throw new ArgumentNullException(); // this exception is thrown  
                                          // regardless of the value of 'path'
    }


    // code to open and parse file
    // returns XElement
}
更新:

在我的测试场景中,调用方法只是发送我为测试硬编码的默认路径。我尚未完成UI,因此用户定义路径的代码不完整

private const string c_fooPath = "C:\\test\\text.txt"

public void CallingFoo()
{
    var xml = Foo(c_fooPath)

    // some code

}
更新#2:

我还试过其他一些测试。我试过了

if (String.IsNullOrEmpty(path))
{
    Console.WriteLine("testing")       // this line is skipped when my condition is
                                       // false but runs when  i force it to be true

    throw new ArgumentNullException(); // this exception is thrown  
                                       // regardless of the value of 'path'
}

if (false)
{
    throw new ArgumentNullException(); // the exception is not thrown here - the only
                                       // condition i have found so far.
}

public static XElement Foo(String path)
{
    path = "test";

    if (String.IsNullOrEmpty(path))
    {
       throw new ArgumentNullException(); // exception is still thrown  
    }


    // code to open and parse file
    // returns XElement
}

我已经给了你的代码一个快速测试,它的工作正如预期的那样,我。E仅在字符串为null或空时引发异常。调试您的代码,看看给定的路径是否确实有内容,并且确实不是空的或空的。可能函数的调用者也有问题

My testcode(返回void而不是XElement):

您还可以尝试使用
Convert.ToString((object)stringVar)==“而不是
String.IsNullOrEmpty

更新:可能有帮助。

试试以下方法:

private const c_fooPath = @"C:\test\text.txt"

public void CallingFoo()
{
    var xml = Foo(c_fooPath)

    // some code

}
声明带有斜杠(\)的字符串变量时,请使用以下任一方法:

1)
private const c_fooPath=@“c:\test\text.txt”

2)
private const c\u fooPath=“c:\\test\\text.txt”

希望有帮助


谢谢。

你好。请包含对此方法Foo的调用。那就更有意义了。另外,它的字符串为.Empty,否则为:)。谢谢。您确定您已经测试了字符串包含数据的条件吗?这似乎只应该抛出null或empty。。。我觉得不错。尝试将字符串设置为if语句前面的值,然后查看它是否仍然抛出。@Vaibhav what is string.Empty您在哪个上下文中提到这个?您确定它会抛出异常而不管路径如何吗?尝试用if(False)替换if(String.IsNullOrEmpty(path)),看看它是否仍然抛出异常。@Vaibhav-谢谢更正了输入错误-这就是我凌晨1点问这个问题的结果。我不认为调用方法是必要的,因为我在问为什么不管字符串的值是多少都会抛出抛出行。谢谢Vaibhav,我实际上有“C:\\test\\text.txt”,在我匆忙中,我只是忘记了包含它们。Thanks@psubsee2003-不是问题。尝试了您在控制台应用程序中提供的代码片段,效果很好:)谢谢macs。根据您的回答,我决定注释掉该方法的其余部分,并将其设置为只返回一个
null
。它的行为是正确的,所以我的问题在我的方法中的其他地方。这根本没道理。谢谢。@psubsee2003:我提供了两个链接,也许这些链接很有用。特别是第一个,似乎也考虑到了类似的问题。
private const c_fooPath = @"C:\test\text.txt"

public void CallingFoo()
{
    var xml = Foo(c_fooPath)

    // some code

}