c# - define new excptions

If appropriate, you should best use built-in exceptions if the meansing is obvious. however, you do have the need to define your own set of exception. The custom exception that you defined may/may not have extra fields/information, because an exception type itself is meaningful enough in certain situation.

 

Suppose you do, let see an simple exception defintion, which does not have any extra information, here is the definition.

 

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

    public CustomException(SerializationInfo info, StreamingContext context)
      : base(info, context)
    { }
  }

 

 

Severl points worth mentioning:

 

 

  • There are several overloads, normally you would define a void, a string message only, and a string message with an inner message - in total three overloads.
  • If your exception needs to communicate with others (cross machine/process) boundary, you may want to provide one that has the Serialization and StreamingContext to marshall the exception.
  • if you decide to have Serialization and StreamingContext, you might want to make your Exception serialzable
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
eslint-define-config 是一个 ESLint 的配置定义模块。ESLint 是一个现代化的 JavaScript 代码检查工具,它可以帮助我们保持代码质量和一致性。eslint-define-config 提供了一个简单的方式来定义我们的 ESLint 配置。 使用 eslint-define-config,我们可以将不同的规则和插件以模块化的方式组合在一起,形成一个可复用的配置。我们可以根据自己项目的需求,定义出适合的规则集合。这样,我们不再需要手动在每个项目中分别配置 ESLint,只需引入我们定义好的配置即可。 定义一个 eslint-define-config 配置非常简单。我们可以使用 JavaScript 或 JSON 文件来编写我们的配置文件。在配置文件中,我们可以指定要使用的规则、插件以及其他相关配置项。我们还可以根据需要对规则进行特定的修改或禁用。一旦我们的配置文件定义完成,我们只需将其导出,然后在项目中使用即可。 使用 eslint-define-config 带来的好处是可以提高代码质量和一致性的同时,也提高了项目开发的效率。我们可以将一些常用的配置集中管理,避免了在每个项目中重复配置的工作。同时,我们也可以更容易地修改和升级我们的规则集合,而不必在每个项目中手动修改。 总之,eslint-define-config 是一个让我们更加轻松地定义和管理 ESLint 配置的工具。它让我们能够以一种模块化并可复用的方式来定义和使用我们的配置,减少了重复的工作并提高了项目开发的效率。通过合理配置和使用 ESLint,我们可以确保代码质量和一致性,从而改善我们的开发体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值