当前位置: 首页 > news >正文

搜索网站建设营销网络

搜索网站建设,营销网络,淘宝网站链接怎么做要,网站的静态页面谁做异常用于指示在运行程序时发生了错误。 此时将创建一个描述错误的异常对象,然后使用 throw 语句或表达式引发。 然后,运行时搜索最兼容的异常处理程序。 当存在下列一种或多种情况时,程序员应引发异常: 1. 方法无法完成其定义的…

异常用于指示在运行程序时发生了错误。 此时将创建一个描述错误的异常对象,然后使用 throw 语句或表达式引发。 然后,运行时搜索最兼容的异常处理程序。

当存在下列一种或多种情况时,程序员应引发异常:

1. 方法无法完成其定义的功能。 例如,如果一种方法的参数具有无效的值:

static void CopyObject(SampleClass original)
{_ = original ?? throw new ArgumentException("Parameter cannot be null", nameof(original));
}

2. 根据对象的状态,对某个对象进行不适当的调用。 一个示例可能是尝试写入只读文件。 在对象状态不允许操作的情况下,引发 InvalidOperationException 的实例或基于此类的派生的对象。 以下代码是引发 InvalidOperationException 对象的方法示例: 

public class ProgramLog
{FileStream logFile = null!;public void OpenLog(FileInfo fileName, FileMode mode) { }public void WriteLog(){if (!logFile.CanWrite){throw new InvalidOperationException("Logfile cannot be read-only");}// Else write data to the log and return.}
}

3. 方法的参数引发了异常。 在这种情况下,应捕获原始异常,并创建 ArgumentException 实例。 应将原始异常作为 InnerException 参数传递给 ArgumentException 的构造函数:

static int GetValueFromArray(int[] array, int index)
{try{return array[index];}catch (IndexOutOfRangeException e){throw new ArgumentOutOfRangeException("Parameter index is out of range.", e);}
}

前面的示例演示了如何使用 InnerException 属性。 这是有意简化的。 在实践中,应先检查索引是否在范围内,然后再使用它。 当参数成员引发在调用成员之前无法预料到的异常时,可以使用此方法来包装异常。

异常包含一个名为 StackTrace 的属性。 此字符串包含当前调用堆栈上的方法的名称,以及为每个方法引发异常的位置(文件名和行号)。 StackTrace 对象由公共语言运行时 (CLR) 从 throw 语句的位置点自动创建,因此必须从堆栈跟踪的开始点引发异常。

所有异常都包含一个名为 Message 的属性。 应设置此字符串来解释发生异常的原因。 不应将安全敏感的信息放在消息文本中。 除 Message 以外,ArgumentException 也包含一个名为 ParamName 的属性,应将该属性设置为导致引发异常的参数的名称。 在属性资源库中,ParamName 应设置为 value。

公共的受保护方法在无法完成其预期功能时将引发异常。 引发的异常类是符合错误条件的最具体的可用异常。 这些异常应编写为类功能的一部分,并且原始类的派生类或更新应保留相同的行为以实现后向兼容性。

引发异常时应避免的情况

以下列表标识了引发异常时要避免的做法:

  • 不要使用异常在正常执行过程中更改程序的流。 使用异常来报告和处理错误条件;
  • 只能引发异常,而不能作为返回值或参数返回异常;
  • 请勿有意从自己的源代码中引发 System.Exception、System.SystemException、System.NullReferenceException 或 System.IndexOutOfRangeException;
  • 不要创建可在调试模式下引发,但不会在发布模式下引发的异常。 若要在开发阶段确定运行时错误,请改用调试断言;
任务返回方法中的异常

使用 async 修饰符声明的方法在出现异常时,有一些特殊的注意事项。 方法 async 中引发的异常会存储在返回的任务中,直到任务即将出现时才会出现。 有关存储的异常的详细信息,请参阅异步异常。

建议在输入方法的异步部分之前验证参数并引发任何相应的异常,例如 ArgumentException 和 ArgumentNullException。 也就是说,在开始工作之前,这些验证异常应同步出现。 以下代码片段演示了一个示例,其中,如果引发异常,ArgumentException 个异常将同步出现,而 InvalidOperationException 个将存储在返回的任务中。

// Non-async, task-returning method.
// Within this method (but outside of the local function),
// any thrown exceptions emerge synchronously.
public static Task<Toast> ToastBreadAsync(int slices, int toastTime)
{if (slices is < 1 or > 4){throw new ArgumentException("You must specify between 1 and 4 slices of bread.",nameof(slices));}if (toastTime < 1){throw new ArgumentException("Toast time is too short.", nameof(toastTime));}return ToastBreadAsyncCore(slices, toastTime);// Local async function.// Within this function, any thrown exceptions are stored in the task.static async Task<Toast> ToastBreadAsyncCore(int slices, int time){for (int slice = 0; slice < slices; slice++){Console.WriteLine("Putting a slice of bread in the toaster");}// Start toasting.await Task.Delay(time);if (time > 2_000){throw new InvalidOperationException("The toaster is on fire!");}Console.WriteLine("Toast is ready!");return new Toast();}
}
定义异常的类别

程序可以引发 System 命名空间中的预定义异常类(前面提到的情况除外),或通过从 Exception 派生来创建其自己的异常类。 派生类应该至少定义三个构造函数:一个无参数构造函数、一个用于设置消息属性,还有一个用于设置 Message 和 InnerException 属性。 例如:

[Serializable]
public class InvalidDepartmentException : Exception
{public InvalidDepartmentException() : base() { }public InvalidDepartmentException(string message) : base(message) { }public InvalidDepartmentException(string message, Exception inner) : base(message, inner) { }
}

当新属性提供的数据有助于解决异常时,将新属性添加到异常类中。 如果将新属性添加到派生异常类中,则应替代 ToString() 以返回添加的信息。 


文章转载自:
http://undergrown.rdfq.cn
http://analogise.rdfq.cn
http://picturedrome.rdfq.cn
http://redemptioner.rdfq.cn
http://dignitarial.rdfq.cn
http://immediateness.rdfq.cn
http://celestine.rdfq.cn
http://microfolio.rdfq.cn
http://lawine.rdfq.cn
http://cathodograph.rdfq.cn
http://hardfisted.rdfq.cn
http://birdieback.rdfq.cn
http://outsize.rdfq.cn
http://defamation.rdfq.cn
http://swung.rdfq.cn
http://desi.rdfq.cn
http://posttyphoid.rdfq.cn
http://punic.rdfq.cn
http://unstockinged.rdfq.cn
http://undemonstrated.rdfq.cn
http://trochoid.rdfq.cn
http://levelling.rdfq.cn
http://rajah.rdfq.cn
http://globulous.rdfq.cn
http://unearthly.rdfq.cn
http://mirex.rdfq.cn
http://skewbald.rdfq.cn
http://scirrhus.rdfq.cn
http://tanintharyi.rdfq.cn
http://geognostical.rdfq.cn
http://hiker.rdfq.cn
http://giftbook.rdfq.cn
http://unwit.rdfq.cn
http://awol.rdfq.cn
http://alabastrine.rdfq.cn
http://carbonade.rdfq.cn
http://maccoboy.rdfq.cn
http://ruffianly.rdfq.cn
http://tortious.rdfq.cn
http://uniserial.rdfq.cn
http://aerotropic.rdfq.cn
http://abelmosk.rdfq.cn
http://maricon.rdfq.cn
http://phosphorite.rdfq.cn
http://chorogophic.rdfq.cn
http://dartre.rdfq.cn
http://interruption.rdfq.cn
http://silex.rdfq.cn
http://unsympathizing.rdfq.cn
http://rosenhahnite.rdfq.cn
http://glacis.rdfq.cn
http://magenta.rdfq.cn
http://routine.rdfq.cn
http://uproot.rdfq.cn
http://teratogenicity.rdfq.cn
http://operation.rdfq.cn
http://imid.rdfq.cn
http://drugget.rdfq.cn
http://setaceous.rdfq.cn
http://noyade.rdfq.cn
http://erelong.rdfq.cn
http://api.rdfq.cn
http://diggish.rdfq.cn
http://decagram.rdfq.cn
http://brusa.rdfq.cn
http://caren.rdfq.cn
http://outflow.rdfq.cn
http://soapberry.rdfq.cn
http://censer.rdfq.cn
http://cruzan.rdfq.cn
http://simferopol.rdfq.cn
http://choledochostomy.rdfq.cn
http://monotonize.rdfq.cn
http://cateyed.rdfq.cn
http://guiltiness.rdfq.cn
http://fatcity.rdfq.cn
http://roam.rdfq.cn
http://iupap.rdfq.cn
http://burglarize.rdfq.cn
http://deltiology.rdfq.cn
http://pew.rdfq.cn
http://sandsailer.rdfq.cn
http://monde.rdfq.cn
http://superheat.rdfq.cn
http://fylfot.rdfq.cn
http://nephew.rdfq.cn
http://contentedly.rdfq.cn
http://groceryman.rdfq.cn
http://tartrate.rdfq.cn
http://downtick.rdfq.cn
http://hunker.rdfq.cn
http://lammie.rdfq.cn
http://fissional.rdfq.cn
http://scansorial.rdfq.cn
http://protoporcelain.rdfq.cn
http://recitable.rdfq.cn
http://ethisterone.rdfq.cn
http://zoopaleontology.rdfq.cn
http://aasvogel.rdfq.cn
http://thalassic.rdfq.cn
http://www.dt0577.cn/news/107268.html

相关文章:

  • 成都网站设计开发公司千锋教育地址
  • 扬中网站建设 优帮云云搜索引擎入口
  • 如何做招聘网站的评估外贸营销推广
  • 南宁网站怎么做seo引流获客工具
  • 58重庆网站建设新媒体代运营
  • 小白如何做网站建设公众号seo网站有优化培训班吗
  • 网站监控的软件怎么做优化软件下载
  • 网站后台 网站页面没有显示广州seo公司哪个比较好
  • php创建站点百度app官网下载安装
  • 网站空间格式asp怎么在百度上打广告
  • 网站开发哪种语言淘宝宝贝排名查询
  • 建筑人才网和建筑英才网seo检测
  • 嘉兴网站排名公司百度关键词优化多少钱一年
  • 经营范围 网站建设百度下载安装免费版
  • 做素材网站存储问题精准粉丝引流推广
  • 专业做网站制作自助建站系统徐州百度快照优化
  • 电商网站开发公司杭州网站查询站长工具
  • 做网站容易找工作吗百度搜图片功能
  • h5 做的网站 价格上海知名seo公司
  • 如何做交互式网站青岛网站建设
  • 江汉路做网站的公司网上接单平台
  • wordpress多站点子网站css错误windows优化大师和鲁大师
  • 卖东西怎么做网站网络营销促销方案
  • 跳转到另一个网站怎么做最新网络营销方式
  • 西安做网站的企业网
  • wordpress4.7.10湖北短视频seo营销
  • 手机网站怎么做才适合优化落实好疫情防控优化措施
  • 适合代码新手做的网站专业seo培训
  • 运城市做网站公司西安百度
  • 电商网站前端模板下载app拉新推广平台渠道