我有一个具有10个属性的类OrderRequestModel。
如果任何属性为null,但如果检查,则不希望执行10次检查,我希望抛出异常。
我怎么能聪明地做这件事?
if (model is DeliveryRequestModel)
{
if (model.Items == null || !model.Items.Any())
{
throw (new ArgumentException("За доставка трябва да има поне един арти
我需要使用锁构造,并编辑以下方法来并行执行:
public void Withdraw(int amountToWithdraw)
{
if (amountToWithdraw <= 0)
{
throw new ArgumentException("The amount should be greater than 0.");
}
if (amountToWith
我想检查传递给类型的构造函数的参数是否有效。
如果无效,我将检查它并引发ArgumentException。
我想为这种行为创建一个测试。我想使用Assert.throws,或者最好使用FSUnit,而不是try/with块。
#package "FsUnit@3.4.1"
#package "nunit@3.11.0"
open System
open FSUnit
type configuration = {aaa:int}
type Client(conf:configuration) =
do
if conf.aaa <
据我所见,ArgumentExceptions通常是这样使用的:
public void UpdateUser(User user)
{
if (user == null) throw new ArgumentException("user");
// etc...
}
但如果我有这样的东西
public void UpdateUser(int idOfUser)
{
var user = GetUserById(idOfUser);
if (user == null) throw new ArgumentException("idOfU
在检查作为string传递的参数时,您将抛出ArgumentException。
void CheckName(string name)
{
if (name != "Alice")
{
throw new ArgumentException("Name is not Alice.");
}
}
但是,当将模型(对象)传递给函数时,您检查了模型的属性并希望抛出异常。抛出哪个异常是正确的?它还是ArgumentException吗
void CheckUser(UserModel model)
{
if (model
好吧,我知道我漏掉了什么东西。我有以下操作合同:
public double DivideByZero(int x, int y)
{
if (y == 0)
{
throw new FaultException<ArgumentException>
(new ArgumentException("Just some dummy exception")
,new FaultReason("some very bogus reason"),
我在我的应用程序中使用dotnetzip,我收到了一份关于崩溃的问题报告,其中包含“(‘和’)”文件。
using (ZipFile zip = new ZipFile("test.zip"))
{
var e = zip.SelectEntries("test (').txt"); //System.ArgumentException
e = zip.SelectEntries("name = test (').txt"
我将我们的应用程序从.NET 3.1Core升级到.NET 6,并尝试使用新的ArgumentNullException.ThrowIfNull();
但是在我们的代码中,我们不仅检查null,还检查其他内容。
if (anyParam == null)
{
throw new ArgumentNullException(nameof(anyParam));
}
if (string.IsNullOrEmpty(stringParam))
{
throw new ArgumentException(nameof(stringParam));
}
if (intParam &
我需要调整IList<T>的大小。我已经写了两个版本,希望有一个回顾。我特别感兴趣的是这两种意见中哪一种更可取,但也对所有其他建议表示感谢。
using System;
using System.Collections.Generic;
using System.Linq;
namespace Foo
{
public static class IListExtensions
{
public static void Resize<T>(this IList<T> list, int size)
{
我刚刚遇到了一个例子,它使用了一个使用收益率返回的方法,而不是抛出我所期望的ArgumentException。我在这里用最简单的类重构了这个例子:
class Program
{
static void Main(string[] args)
{
try
{
var listA = FooA(count: 0);
Console.WriteLine("A did not throw exception!");
}
catch (ArgumentExc
我编写了一个函数,它可能引发各种异常。
public class MyClass
{
private void Test(string param)
{
if (param.Length > 10)
throw new ArgumentException();
else if (param.Length > 20)
throw new OverflowException();
else if (string.
我正在查看一些旧代码,我很好奇是否应该在setter属性中使用逻辑,或者是否有更好的方法来编写这些代码,为什么呢?谢谢!
string qBankCode;
string qCode;
public string QBankCode
{
get => qBankCode;
set
{
if (value.Length == 0)
throw new ArgumentException
我有一个单元测试失败了,因为一个System.ArgumentException正在被抛出,即使我在期待,而且它是故意的行为--我错过了什么?
[Test]
[ExpectedException(typeof(ArgumentException), ExpectedMessage = "Seconds from midnight cannot be more than 86400 in 010100712386401000000012")]
public void TestParsingCustomReferenceWithInValidSecondsFromMidnight(
执行以下操作来组织代码--特别是异常--有什么错误吗?
public class QueryParameterException
{
public class BooleanArgumentException : ArgumentException
{
public BooleanArgumentException(string message)
: base(message)
{
}
}
public class DoubleArgumentException : ArgumentExc
如果我有一个可以在两个地方抛出ArgumentException的例程,比如...
if (Var1 == null)
{
throw new ArgumentException ("Var1 is null, this cannot be!");
}
if (Val2 == null)
{
throw new ArgumentException ("Var2 is null, this cannot be either!");
}
在我的调用过程中,确定抛出两个异常中的哪一个异常的最佳方法是什么?
或
我做这件事的方式错了吗?
我有以下方法,其中包括在开始时的验证检查。我正在使用NLog,我想记录异常消息并同时抛出异常,尽可能避免代码膨胀。
目前,我做了以下工作,但似乎有点笨重。有更好的办法吗?
public static void ValidateValue(string value)
{
if (!string.IsNullOrWhiteSpace(value) && value.Contains(","))
{
ArgumentException ex = new ArgumentException(string.Format("value
我在使用PowerShell复制注册表子树时遇到了困难,因为有些键包含斜杠(例如HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\11.0\Languages\Language Services\C/C++)
PS > Copy-Item -recurse HKCU:\Software\Microsoft\VisualStudio\11.0\Languages HKCU:\ZZZ
Copy-Item : The registry key at the specified path does not exist.
At line:1 ch
我得到了这个类,它引发了相同类型的异常,我如何捕获这个异常并显示相应的错误消息。这就是我现在要做的。
public bool ChangePassword(oldPassword,newPassword)
{
if(oldPassword != savedInDatabase)
{
throw new ArgumentException("Your old password is not same as one saved in our database")
}
if(string.IsNullOrEmpty(oldPassword) || string.IsN
我仍然在寻找领域模型验证的最佳实践。把验证放在域模型的构造函数中好吗?我的域模型验证示例如下:
public class Order
{
private readonly List<OrderLine> _lineItems;
public virtual Customer Customer { get; private set; }
public virtual DateTime OrderDate { get; private set; }
public virtual decimal OrderTotal { get; private se
我正在尝试为我的API包装一个遗留服务。在服务中执行CRUD操作的域模型抛出显式标准异常(如ArgumentException或AggregateException ),同时也通过失败的存储过程隐式抛出它们。我不希望通过包装所有异常来公开有关数据库或程序的信息,我只想捕捉那些由过去的开发人员抛出的异常。
在避免从失败的存储过程中抛出隐式throw new ArgumentException()时,捕获显式ArgumentException的最佳方法是什么?
我使用try和catch语句作为保持代码运行而不会崩溃的一种简单方法(我会将所有内容都包装在一个大的try中)。最近,我想开始更正确地使用try和catch语句。作为一个例子,我对以下内容有疑问:
public class Ninja{
Ninja(){
}
public void ThrowShirikin(int numberOfShirikins){
try{
if(numberOfShirikins == 0){
throw new System.ArgumentException(
在一个侧面加载的Windows应用商店应用的日志中,我发现了以下异常: Arg_ArgumentException --> System.ArgumentException: Arg_ArgumentException at SharedLibrary!<BaseAddress>+0x3a13ad at my-app!<BaseAddress>+0xf29d82h at my-app!<BaseAddress>+0xf29cdc at my-app!<BaseAddress>+0x10e0844 at my-