zsm415460830 2015-11-23 14:33 采纳率: 66.7%
浏览 1832
已采纳

C#异常处理相关,try catch

 using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication7
{
    class Photo
{//Photo类

    string _title;

    public Photo(string title)
    {//构造函数
            this._title = title;
    }

    public string Title
    {//访问器
        get
        {
            return _title;
        }
    }
    public override string ToString()
    {
        try
        {
            return _title;
        }
        catch (NullReferenceException e)
        {
            throw new NullReferenceException("Not Found");
        }
    }
}

    class Album
    {
        // 该数组用于存放照片
        Photo[] photos;
        public Album(int capacity)
        {
            photos = new Photo[capacity];
        }

        public Photo this[int index]
        {//索引器
            set { 
                if (index<0 || index>=photos.Length)  
                    Console.WriteLine("Wrong Index");
                else
                    photos[index] = value; }
            get {
                if (index < 0 || index >= photos.Length)
                    return null;
                return photos[index]; }
        }

        public Photo this[string str]
        {//索引器 
            get {
                    int i = 0;
                    while (i < photos.Length)
                    {
                        if (photos[i].ToString() == str)
                            return photos[i];
                        i++;
                    };
                    return null;
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // 创建一个容量为 3 的相册
            Album family = new Album(3);
            // 创建 3 张照片
            Photo first = new Photo("Jeny ");
            Photo second = new Photo("Smith");
            Photo third = new Photo("Lono");
            // 向相册加载照片
             family[0] = first;
             family[1] = second;
             family[2] = third;
            // 按索引检索
            Photo objPhoto1 = family[2]; 
            Console.WriteLine(objPhoto1.Title);
            // 按名称检索
            Photo objPhoto2 = family["Jeny"];
            Console.WriteLine(objPhoto2.Title);
            Console.Read();
        }    

    }
}

运行后报错,原因在于Photo objPhoto2 = family["Jeny"]中"Jeny"与"Jeny "(Jeny后有空格)不相等,索引器返回值为null。此时objPhoto2为null,不能执行Console.WriteLine(objPhoto2.Title);

请问这个异常是谁呢么类型的异常?我该如何使用异常处理,使其输出“Not Found”?
如果不是在自己重写的Tostring()函数中,那么应当在那里捕获这个异常?

第一次提问,略惶恐,先谢谢前辈们

  • 写回答

2条回答 默认 最新

  • havedream_one 2015-11-23 16:15
    关注

    在toString中没有必要使用try...catch
    完全可以if判断下objPhoto2是否null
    我感觉try...catch没有必要刻意去使用,简单的用if就行,真的是有必要的时候用才是最合理的
    真想用的话
    try
    {
    Console.WriteLine(objPhoto2.Title);;
    }
    catch (NullReferenceException e)
    {
    throw new NullReferenceException("Not Found");
    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog