简体   繁体   English

不使用 catch 块捕获的异常。 为什么?

[英]Exceptions not catching using catch block. Why?

So I have made use of a few try/catch blocks during my first project learning C#.因此,在我学习 C# 的第一个项目中,我使用了一些 try/catch 块。 For the most part, they have worked fine.在大多数情况下,他们工作得很好。 However, there are a couple where the code still breaks at the point of an exception despite the fact it is finding the exact exception type I was anticipating and hoping for.然而,有几个代码仍然在异常点中断,尽管事实上它找到了我预期和希望的确切异常类型。 I'll provide an example of code where this issue occurs.我将提供发生此问题的代码示例。

Here is where I try to catch the exception:这是我尝试捕获异常的地方:

app.MoveFolder(input1, input2);

try {
    //code here
} catch (ArgumentException) {
    //code here
}
break;

Here is the function where I create the exception:这是我创建异常的函数:

public void MoveFolder(string folderPath, string newLocation) {
    this.ThrowExceptionIfFolderDoesntExist(folderPath);

    if (Directory.Exists(newLocation) == true) {
        throw new ArgumentException("example");
    }
    Directory.Move(folderPath, newLocation);
}

The ThrowExceptionIfFolderDoesntExist() function leads to this: ThrowExceptionIfFolderDoesntExist()函数导致:

private void ThrowExceptionIfFolderDoesntExist(string folderPath) {
    if (this.CheckFolderExists(folderPath) == false) {
        throw new ArgumentException("This folder does not exist");
    }
}

So, as you can see, both this and the if statement in my MoveFolder() function should return ArgumentExceptions which I was hoping to catch.因此,如您所见,我的MoveFolder()函数中的此语句和 if 语句都应返回我希望捕获的ArgumentExceptions In the case of the latter function, this works as intended.在后一个函数的情况下,这按预期工作。 However, if I try to move a folder to a location that already exists, then I get the following:但是,如果我尝试将文件夹移动到已存在的位置,则会得到以下信息:

Unhandled Exception: System.ArgumentException: example

This is not what I'm wanting, as I want the catch block to handle this particular ArgumentException also.这不是我想要的,因为我希望 catch 块也能处理这个特定的 ArgumentException。 Is this something to do with the catch block thinking I am referring to a specific argument exception?这与认为我指的是特定参数异常的 catch 块有关吗? I would have thought it would refer to all ArgumentExceptions.我原以为它会引用所有 ArgumentExceptions。

How can I resolve this?我该如何解决这个问题?

In order to do a proper try/catch, you need to do the following:为了正确执行 try/catch,您需要执行以下操作:

  1. Place your method in the try block将您的方法放在try块中
  2. catch the Exception(s) catch Exception(s)
  3. throw the Exception throw Exception

Here is some code这是一些代码

try
{
    app.MoveFolder(input1, input2);
}
// catch ArgumentException
catch(ArgumentException ex)
{
    throw;
}
// catch all others
catch(Exception ex)
{
    throw;
}

In order to catch an exception thrown by a function, the function needs to be called in the Try {}.为了捕获函数抛出的异常,需要在 Try{} 中调用该函数。 You can do as the others said and call the MoveFolder inside a Try Block.您可以按照其他人的说法进行操作,并在 Try 块中调用 MoveFolder。 If you throw an exception outside of the Try Block, it can sometimes be picked up by other catch blocks, or just unhandled and creates an error.如果您在 Try 块之外抛出异常,它有时会被其他 catch 块拾取,或者只是未处理并产生错误。

try {
    app.MoveFolder(Something1, Something2);
} catch (ArgumentException ex) {
    //Do something with Exception
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM