在catch块中引发异常

在catch块中引发异常

本文介绍了在catch块中引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解如何在返回值的方法中处理异常,在我的情况下,返回值是Person []类型的值。我尝试按照此处的说明进行操作-,但我仍然遇到例外-抛出icex;线。有人可以给我一个提示吗?
我也尝试在catch块中返回null而不是throw,但是我只得到另一个异常。(我故意使用ArrayList而不是List)

 静态ArrayList CreateNonGenericList()
{
ArrayList personList = new ArrayList()
{
new Person {FirstName = John,LastName = Clark,Age = 39,
StartDate = new DateTime(1989,12,30)},
new Person {FirstName = Zefa,LastName = Thoms,Age = 23,
StartDate = new DateTime(2003,4,12)},
new Person {FirstName = Robin,LastName = Hood,Age = 33,
StartDate = new DateTime(2001, 4,12)}
};
personList.Add( John); //传递一个字符串值而不是Person
return personList;
}

静态Person [] SortNonGenericList(ArrayList personList)
{
try
{
Person [] LatestpersonList =(from Person p inpersonList
其中p.StartDate> new DateTime(2000,1,1)
选择p).ToArray();
return LatestpersonList;
}
捕获(例如InvalidCastException)
{
InvalidCastException icex = new InvalidCastException(ex.Message,ex);
投冰; //在这里获取InvalidCastException
}
}


解决方案

如果只想让方法的调用者处理该异常,则可以完全删除 try / catch 块。



如果您想在 catch

>阻止(例如记录),您应该抛出原始异常:

  catch(InvalidCastException ex)
{
//日志(ex);
投掷;
}

这样,异常中的堆栈跟踪不会像您一样重置当前代码。



正如其他人指出的那样,您当前正在做的事情是没有用的,因为您正在抛出具有相同类型和消息的 new 异常。创建新的异常可能很有用,但是例如,如果您想要更具描述性的异常:

  catch(InvalidCastException ex)
{
throw new ApplicationException(无法排序列表,因为至少一个人没有StartDate,例如);
}

然后,该异常仍会在catch块中出现,但是其描述然后会在代码中为该位置提供有用的信息。



当然,最后,您实际上必须处理异常。如果无法对personList进行排序,该怎么办?以原始顺序归还它们吗?退出应用程序?告诉最终用户操作失败?


I can't understand how to handle an exception in a method which returns value, in my case the value of Person[] type. I tried to do as written here - Creating and Throwing Exceptions, but I'm still getting an exception in - throw icex; line. Could someone please give me a hint?I also tried to return null in catch block, instead of throw, but I only get another exception.(I'm using ArrayList instead of List intentionally)

static ArrayList CreateNonGenericList()
    {
        ArrayList personList = new ArrayList()
            {
                new Person {FirstName="John", LastName="Clark", Age=39,
                    StartDate= new DateTime(1989, 12, 30)},
                new Person{FirstName="Zefa", LastName="Thoms", Age=23,
                    StartDate= new DateTime(2003, 4, 12)},
                new Person{FirstName="Robin", LastName="Hood", Age=33,
                    StartDate= new DateTime(2001, 4, 12)}
            };
        personList.Add("John"); //Passing a String value instead of Person
        return personList;
    }

    static Person[] SortNonGenericList(ArrayList personList)
    {
        try
        {
            Person[] latestpersonList = (from Person p in personList
                                         where p.StartDate > new DateTime(2000, 1, 1)
                                         select p).ToArray();
            return latestpersonList;
        }
        catch (InvalidCastException ex)
        {
            InvalidCastException icex = new InvalidCastException(ex.Message, ex);
            throw icex; //Getting an InvalidCastException here
        }
    }
解决方案

If all you want to do is let the caller of your method to handle the exception, you can remove the try/catch block entirely. Exceptions will "bubble up" automatically when they are not caught.

If you would like to do something in the catch block (such as logging) you should throw the original exception:

catch (InvalidCastException ex)
{
    // Log(ex);
    throw;
}

This way the stack trace in the exception is not "reset" as in your current code.

As others have pointed out, what you're currently doing is useless because you're throwing a new exception with the same type and message. Creating a new exception can be useful though if for instance you want a more descriptive exception:

catch (InvalidCastException ex)
{
    throw new ApplicationException("Unable to Sort list because at least one person has no StartDate", ex);
}

The exception would then still "occur" in the catch block, but its description would then provide useful information for that location in the code.

Of course in the end you would have to actually handle the exception. What do you want to do if you can't sort the personList? Return them in their original order? Quit the application? Tell the end user that the operation has failed?

这篇关于在catch块中引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 23:04