C#中未引发异常 [英] exception is not thrown in c#

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

问题描述

感谢先前的帮助.

我调用以下函数并传递一个不在字符串resuorce文件中的字符串值"string8".然后,为什么我的以下代码没有抛出异常并显示任何消息,例如未找到"之类的消息,所以任何逻辑都是错误的我的尝试抓住了吗?

Thanks for the previous help.

I call the following function and pass a string value "string8" which is not in the resuorce file already.then how come my following code is not throwing an exeption and show any message saying something like" its not found" any logic is wrong with my try catch?

public void printmessage(string value)
{
   try
   {
      string x = (string)GetGlobalResourceObject(Resource1, value);
       Label1.Text = string.Format("{0}", x);
    }
    catch (Exception ex)
    {
       Response.Write(ex.ToString());
    }
}

推荐答案

在try
的第一行上放置一个断点 string x = ...(这个)
然后调试您的应用程序.如果应用程序在该行中断,则检查Resource1的值和value,然后转到下一行,查看它是否进入catch块或是否在Label文本集行上.再次检查x和ex的值,具体取决于逻辑将您带到何处.到那时,您应该更好地了解问题所在.
Place a breakpoint on the first line inside the try
string x = ... (this one)
Then debug your application. If and when the application breaks on that line, check the values of Resource1 and value, then step to the next line and see if it breaks into the catch block or if it goes on the the Label text set line. Again check the values, now of x and ex depending on where the logic takes you. At that point you should have a better understanding of what your problem is.


GetGlobalResourceObject:
它返回一个代表请求的资源对象的对象,否则,如果未找到资源对象或找到资源对象但不具有请求的属性,则返回null.
GetGlobalResourceObject:
it returns an object that represents the requested resource object otherwise, null if a resource object is not found or if a resource object is found but it does not have the requested property.
string x = (string)GetGlobalResourceObject(Resource1, value);


因此,当您尝试将null强制转换为字符串时,它什么也没有.
因此,这不会像读取文件中的tat一样发生异常,如果找不到该文件,它将返回异常.

GetGlobalResourceObject抛出的唯一异常是MissingManifestResourceException,该异常返回字符串找不到具有指定classKey参数的资源对象"



如果您的输出显示未找到类似GetGlobalResourceObject的内容,请尝试使用httpcontext类对其进行限定.例如HttpContext.GetGlobalResourceObject()


so when you try to cast a null to a string its nothing.
so this will not be an exception like tat in file read, where it returns an exception if the file not found.

the only exception thrown by the GetGlobalResourceObject is MissingManifestResourceException which returns the string ''A resource object with the specified classKey parameter was not found''



if your output says like GetGlobalResourceObject not found try qualifing that with the httpcontext class. eg HttpContext.GetGlobalResourceObject()


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

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