为什么我们不使用new关键字初始化catch块中的异常引用类型。 [英] why we don't use new keyword to initialize exception reference type in catch block.

查看:87
本文介绍了为什么我们不使用new关键字初始化catch块中的异常引用类型。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为Exception类是一个引用类型,那么我们如何在不使用new关键字初始化异常引用类型的情况下调用suppose Message属性。它们类似于字符串类,它不需要new关键字来初始化对象
(不可变引用类型)。 

as Exception class is an reference type , then how are we able to call suppose Message property without initializing the exception reference type with new keyword . are they similar to string class which doesn't require new keyword to initialize the object ( immutable reference types). 

推荐答案

您好,

e case在使用时被实例化,在本例中来自Demo。 

e in this case is instantiated upon being used, in this case from Demo. 

using System;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Demo("Mary");
            }
            catch (Exception e)
            {
                Console.WriteLine(


" Error {e.Message}");
}
}
static void Demo(string pName)
{
if(pName!=" Karen")
{
throw新例外("非凯伦");
}
}
}
}
"Error {e.Message}"); } } static void Demo(string pName) { if (pName != "Karen") { throw new Exception("Not Karen"); } } } }




 


 


这篇关于为什么我们不使用new关键字初始化catch块中的异常引用类型。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆