From c191639a240d870a3e579a216d6fc3b2e8595e34 Mon Sep 17 00:00:00 2001 From: Adrian Conlon Date: Tue, 26 May 2026 21:00:47 +0100 Subject: [PATCH] Simplify some exception throwing logic --- EightBit/IntelHexFile.cs | 11 ++--------- LR35902/SM83.HarteTest/Cycle.cs | 7 +------ M6502/M6502.HarteTest/Cycle.cs | 7 +------ M6502/M6502.Symbols/ReflectedSectionProperties.cs | 5 +---- Z80/Z80.HarteTest/Cycle.cs | 7 +------ 5 files changed, 6 insertions(+), 31 deletions(-) diff --git a/EightBit/IntelHexFile.cs b/EightBit/IntelHexFile.cs index 5c5653a..45763e5 100644 --- a/EightBit/IntelHexFile.cs +++ b/EightBit/IntelHexFile.cs @@ -34,11 +34,7 @@ namespace EightBit ArgumentNullException.ThrowIfNullOrEmpty(line); var requiredLength = 9 + 2 + (count * 2); - if (line.Length != requiredLength) - { - throw new ArgumentOutOfRangeException(nameof(line), "Invalid hex file: line is not the required length"); - } - + ArgumentOutOfRangeException.ThrowIfNotEqual(line.Length, requiredLength, nameof(line)); var data = new byte[count]; for (var i = 0; i < count; ++i) { @@ -55,10 +51,7 @@ namespace EightBit ArgumentNullException.ThrowIfNullOrEmpty(line); var colon = line[..1]; - if (colon != ":") - { - throw new ArgumentOutOfRangeException(nameof(line), "Invalid hex file: line does not begin with a colon"); - } + ArgumentOutOfRangeException.ThrowIfNotEqual(colon, ":", nameof(line)); var countString = line.Substring(1, 2); var count = Convert.ToByte(countString, 16); diff --git a/LR35902/SM83.HarteTest/Cycle.cs b/LR35902/SM83.HarteTest/Cycle.cs index 4fc56be..db20f30 100644 --- a/LR35902/SM83.HarteTest/Cycle.cs +++ b/LR35902/SM83.HarteTest/Cycle.cs @@ -20,12 +20,7 @@ namespace SM83.HarteTest public Cycle(List input) { Debug.Assert(input is not null, "input cannot be null"); - - if (input.Count != 3) - { - throw new ArgumentOutOfRangeException(nameof(input), input, "Cycles can only have three elements"); - } - + ArgumentOutOfRangeException.ThrowIfNotEqual(input.Count, 3, nameof(input)); this.Address = AsElement(input[0]).GetUInt16(); this.Value = AsElement(input[1]).GetByte(); diff --git a/M6502/M6502.HarteTest/Cycle.cs b/M6502/M6502.HarteTest/Cycle.cs index 8a8ed9f..d39c56f 100644 --- a/M6502/M6502.HarteTest/Cycle.cs +++ b/M6502/M6502.HarteTest/Cycle.cs @@ -20,12 +20,7 @@ namespace M6502.HarteTest public Cycle(List input) { Debug.Assert(input is not null, "input cannot be null"); - - if (input.Count != 3) - { - throw new ArgumentOutOfRangeException(nameof(input), input, "Cycles can only have three elements"); - } - + ArgumentOutOfRangeException.ThrowIfNotEqual(input.Count, 3, nameof(input)); this.Address = AsElement(input[0]).GetUInt16(); this.Value = AsElement(input[1]).GetByte(); this.Type = AsElement(input[2]).GetString(); diff --git a/M6502/M6502.Symbols/ReflectedSectionProperties.cs b/M6502/M6502.Symbols/ReflectedSectionProperties.cs index 732ea1f..cc34b69 100644 --- a/M6502/M6502.Symbols/ReflectedSectionProperties.cs +++ b/M6502/M6502.Symbols/ReflectedSectionProperties.cs @@ -43,10 +43,7 @@ public string GetPropertyNameFromEntryName(string name) { var found = this._entriesToProperties.TryGetValue(name, out var propertyName); - if (!found) - { - throw new ArgumentOutOfRangeException(nameof(name), name, "Missing property mapping"); - } + ArgumentOutOfRangeException.ThrowIfEqual(found, false, nameof(name)); Debug.Assert(propertyName is not null); return propertyName; } diff --git a/Z80/Z80.HarteTest/Cycle.cs b/Z80/Z80.HarteTest/Cycle.cs index 9b11c92..0c316be 100644 --- a/Z80/Z80.HarteTest/Cycle.cs +++ b/Z80/Z80.HarteTest/Cycle.cs @@ -23,12 +23,7 @@ public Cycle(List input) { Debug.Assert(input is not null, "input cannot be null"); - - if (input.Count != 3) - { - throw new ArgumentOutOfRangeException(nameof(input), input, "Cycles can only have three elements"); - } - + ArgumentOutOfRangeException.ThrowIfNotEqual(input.Count, 3, nameof(input)); this.Address = AsElement(input[0]).GetUInt16(); if (input[1] is not null) {