简体   繁体   English

URI无效时应该抛出哪个异常?

[英]Which exception should I throw when a URI isn't valid?

I am currently writing some configuration classes. 我目前正在编写一些配置类。 One of them has a property of type URI. 其中之一具有URI类型的属性。

Within the setter, I am using Uri.IsWellFormedUriString() to validate the Uri. 在设置器中,我正在使用Uri.IsWellFormedUriString()验证Uri。 In case validation fails I'd like to raise an appropriate exception. 万一验证失败,我想提出一个适当的例外。

Which one would you suggest? 您会建议哪一个? Basically I am an advocacy of exceptions which have self-explaining names, like, in this case something like "UriMalformedException", but I am also an advocacy of not cluttering up namespaces with trivial types. 基本上,我提倡使用具有不明原因名称的异常,例如在这种情况下,例如“ UriMalformedException”,但我也提倡不要将琐碎的类型的名称空间弄乱。

Probably https://msdn.microsoft.com/en-us/library/System.UriFormatException(v=VS.110).aspx 可能是https://msdn.microsoft.com/zh-CN/library/System.UriFormatException(v=VS.110).aspx

UriFormatException Class

OR 要么

Create your own exception and throw that. 创建自己的异常并抛出该异常。

I guess I'll follow your proposal, rmn36, although it is not PCL compatible. 我猜我会遵循您的建议,rmn36,尽管它与PCL不兼容。 Throwing an exception and catching another one ( UriFormatException vs. FormatException ) kind of has a "smell" to me, but still... 引发异常并捕获另一种异常( UriFormatExceptionFormatException )对我来说具有“气味”,但是仍然...

Thank you anyway. 还是要谢谢你。

Carsten 卡斯滕

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM