简体   繁体   English

从重载运算符抛出什么异常

[英]What exception to throw from overloaded operator

I made a Matrix implementation which has an overloaded operator * . 我做了一个Matrix实现,它有一个重载的operator * If there're 2 matricies of incompatible sizes I'd like to throw either ArgumentException or InvalidOperationException . 如果有2个不兼容大小的基质我想抛出ArgumentExceptionInvalidOperationException Which one should I use? 我应该使用哪一个? Or maybe it's move convenient to declare my own? 或者也许它可以方便地申报我自己的?

Definitely create your own MismatchingDimensionsException . 绝对创建自己的MismatchingDimensionsException

Your users won't be confused by such an exception, they'll know what the problem is right away. 您的用户不会被这样的例外所困惑,他们会立即知道问题所在。 It's the kind of problem that can happen a lot when dealing with matrices. 在处理矩阵时,这种问题可能会发生很多。

Now, do you derive it from ArgumentException or InvalidOperationException? 现在,您是从ArgumentException还是InvalidOperationException派生出来的? I would say the former, but the latter makes just as much sense. 我会说前者,但后者同样有意义。 If you use your own exception class, it matters less. 如果您使用自己的异常类,则重要性更低。

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

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