Skip to content

Commit 47a9585

Browse files
author
jonas echterhoff
authored
FIX: Fix all exception-related code analyizer warnings (#627)
1 parent 410db9d commit 47a9585

35 files changed

Lines changed: 128 additions & 115 deletions

Assets/Tests/InputSystem/CoreTests_Layouts.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ public void Layouts_AddingTwoControlsWithSameName_WillCauseException()
889889
InputSystem.RegisterLayout(json);
890890

891891
Assert.That(() => InputSystem.AddDevice("MyDevice"),
892-
Throws.TypeOf<Exception>().With.Property("Message").Contain("Duplicate control"));
892+
Throws.TypeOf<InvalidOperationException>().With.Property("Message").Contain("Duplicate control"));
893893
}
894894

895895
[Test]

Packages/com.unity.inputsystem/InputSystem/Actions/InputAction.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -590,8 +590,7 @@ internal int BindingIndexOnActionToBindingIndexOnMap(int indexOfBindingOnAction)
590590
}
591591

592592
throw new ArgumentOutOfRangeException(nameof(indexOfBindingOnAction),
593-
$"Binding index {indexOfBindingOnAction} is out of range for action '{this}' with {currentBindingIndexOnAction + 1} bindings"
594-
);
593+
$"Binding index {indexOfBindingOnAction} is out of range for action '{this}' with {currentBindingIndexOnAction + 1} bindings");
595594
}
596595

597596
/// <summary>

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionAsset.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ public int GetControlSchemeIndex(string name)
427427
{
428428
var index = TryGetControlSchemeIndex(name);
429429
if (index == -1)
430-
throw new Exception($"No control scheme called '{name}' in '{this.name}'");
430+
throw new ArgumentException($"No control scheme called '{name}' in '{this.name}'", nameof(name));
431431
return index;
432432
}
433433

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionMap.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,7 +1089,7 @@ public InputActionMap[] ToMaps()
10891089
var jsonAction = actions[i];
10901090

10911091
if (string.IsNullOrEmpty(jsonAction.name))
1092-
throw new Exception($"Action number {i + 1} has no name");
1092+
throw new InvalidOperationException($"Action number {i + 1} has no name");
10931093

10941094
////REVIEW: make sure all action names are unique?
10951095

@@ -1103,7 +1103,7 @@ public InputActionMap[] ToMaps()
11031103
actionName = actionName.Substring(indexOfFirstSlash + 1);
11041104

11051105
if (string.IsNullOrEmpty(actionName))
1106-
throw new Exception(
1106+
throw new InvalidOperationException(
11071107
$"Invalid action name '{jsonAction.name}' (missing action name after '/')");
11081108
}
11091109

@@ -1167,7 +1167,7 @@ public InputActionMap[] ToMaps()
11671167

11681168
var mapName = jsonMap.name;
11691169
if (string.IsNullOrEmpty(mapName))
1170-
throw new Exception($"Map number {i + 1} has no name");
1170+
throw new InvalidOperationException($"Map number {i + 1} has no name");
11711171

11721172
// Try to find existing map.
11731173
InputActionMap map = null;
@@ -1201,7 +1201,7 @@ public InputActionMap[] ToMaps()
12011201
var jsonAction = jsonMap.actions[n];
12021202

12031203
if (string.IsNullOrEmpty(jsonAction.name))
1204-
throw new Exception($"Action number {i + 1} in map '{mapName}' has no name");
1204+
throw new InvalidOperationException($"Action number {i + 1} in map '{mapName}' has no name");
12051205

12061206
// Create action.
12071207
var action = new InputAction(jsonAction.name)

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionRebindingExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ public static void ApplyBindingOverride(this InputActionMap actionMap, int bindi
125125
throw new ArgumentNullException(nameof(actionMap));
126126
var bindingsCount = actionMap.m_Bindings?.Length ?? 0;
127127
if (bindingIndex < 0 || bindingIndex >= bindingsCount)
128-
throw new ArgumentOutOfRangeException(
129-
$"Cannot apply override to binding at index {bindingIndex} in map '{actionMap}' with only {bindingsCount} bindings", "bindingIndex");
128+
throw new ArgumentOutOfRangeException(nameof(bindingIndex),
129+
$"Cannot apply override to binding at index {bindingIndex} in map '{actionMap}' with only {bindingsCount} bindings");
130130

131131
actionMap.m_Bindings[bindingIndex].overridePath = bindingOverride.overridePath;
132132
actionMap.m_Bindings[bindingIndex].overrideInteractions = bindingOverride.overrideInteractions;
@@ -963,7 +963,7 @@ private void OnComplete()
963963
if (m_TargetBindingIndex >= 0)
964964
{
965965
if (m_TargetBindingIndex >= m_ActionToRebind.bindings.Count)
966-
throw new Exception(
966+
throw new InvalidOperationException(
967967
$"Target binding index {m_TargetBindingIndex} out of range for action '{m_ActionToRebind}' with {m_ActionToRebind.bindings.Count} bindings");
968968

969969
m_ActionToRebind.ApplyBindingOverride(m_TargetBindingIndex, path);

Packages/com.unity.inputsystem/InputSystem/Actions/InputActionSetupExtensions.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,7 @@ public BindingSyntax WithInteraction<TInteraction>()
439439
{
440440
var interactionName = InputProcessor.s_Processors.FindNameForType(typeof(TInteraction));
441441
if (interactionName.IsEmpty())
442-
throw new ArgumentException(
443-
$"Type '{typeof(TInteraction)}' has not been registered as a processor",
444-
"TInteraction");
442+
throw new NotSupportedException($"Type '{typeof(TInteraction)}' has not been registered as a processor");
445443

446444
return WithInteraction(interactionName);
447445
}
@@ -478,9 +476,7 @@ public BindingSyntax WithProcessor<TProcessor>()
478476
{
479477
var processorName = InputProcessor.s_Processors.FindNameForType(typeof(TProcessor));
480478
if (processorName.IsEmpty())
481-
throw new ArgumentException(
482-
$"Type '{typeof(TProcessor)}' has not been registered as a processor",
483-
"TProcessor");
479+
throw new NotSupportedException($"Type '{typeof(TProcessor)}' has not been registered as a processor");
484480

485481
return WithProcessor(processorName);
486482
}

Packages/com.unity.inputsystem/InputSystem/Actions/InputBindingResolver.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public unsafe void AddActionMap(InputActionMap map)
182182

183183
// Make sure that if it's part of a composite, we are actually part of a composite.
184184
if (isPartOfComposite && currentCompositeBindingIndex == InputActionState.kInvalidIndex)
185-
throw new Exception(
185+
throw new InvalidOperationException(
186186
$"Binding '{unresolvedBinding}' is marked as being part of a composite but the preceding binding is not a composite");
187187

188188
// Skip binding if it is disabled (path is empty string).
@@ -359,7 +359,7 @@ public unsafe void AddActionMap(InputActionMap map)
359359
// Make sure the binding is named. The name determines what in the composite
360360
// to bind to.
361361
if (string.IsNullOrEmpty(unresolvedBinding.name))
362-
throw new Exception(
362+
throw new InvalidOperationException(
363363
$"Binding '{unresolvedBinding}' that is part of composite '{composites[currentCompositeIndex]}' is missing a name");
364364

365365
// Give a part index for the
@@ -403,7 +403,7 @@ public unsafe void AddActionMap(InputActionMap map)
403403
// Don't swallow exceptions that indicate something is wrong in the code rather than
404404
// in the data.
405405
if (exception.IsExceptionIndicatingBugInCode())
406-
throw exception;
406+
throw;
407407
}
408408
}
409409

@@ -575,12 +575,12 @@ private int ResolveInteractions(string interactionString)
575575
// Look up interaction.
576576
var type = InputInteraction.s_Interactions.LookupTypeRegistration(m_Parameters[i].name);
577577
if (type == null)
578-
throw new Exception(
578+
throw new InvalidOperationException(
579579
$"No interaction with name '{m_Parameters[i].name}' (mentioned in '{interactionString}') has been registered");
580580

581581
// Instantiate it.
582582
if (!(Activator.CreateInstance(type) is IInputInteraction interaction))
583-
throw new Exception($"Interaction '{m_Parameters[i].name}' is not an IInputInteraction");
583+
throw new InvalidOperationException($"Interaction '{m_Parameters[i].name}' (mentioned in '{interactionString}') is not an IInputInteraction");
584584

585585
// Pass parameters to it.
586586
NamedValue.ApplyAllToObject(interaction, m_Parameters[i].parameters);
@@ -603,13 +603,13 @@ private int ResolveProcessors(string processorString)
603603
// Look up processor.
604604
var type = InputProcessor.s_Processors.LookupTypeRegistration(m_Parameters[i].name);
605605
if (type == null)
606-
throw new Exception(
606+
throw new InvalidOperationException(
607607
$"No processor with name '{m_Parameters[i].name}' (mentioned in '{processorString}') has been registered");
608608

609609
// Instantiate it.
610610
if (!(Activator.CreateInstance(type) is InputProcessor processor))
611-
throw new Exception(
612-
$"Type '{type.Name}' registered as processor called '{m_Parameters[i].name}' is not an InputProcessor");
611+
throw new InvalidOperationException(
612+
$"Type '{type.Name}' registered as processor called '{m_Parameters[i].name}' (mentioned in '{processorString}') is not an InputProcessor");
613613

614614
// Pass parameters to it.
615615
NamedValue.ApplyAllToObject(processor, m_Parameters[i].parameters);
@@ -628,12 +628,12 @@ private static InputBindingComposite InstantiateBindingComposite(string nameAndP
628628
// Look up.
629629
var type = InputBindingComposite.s_Composites.LookupTypeRegistration(nameAndParametersParsed.name);
630630
if (type == null)
631-
throw new Exception(
631+
throw new InvalidOperationException(
632632
$"No binding composite with name '{nameAndParametersParsed.name}' has been registered");
633633

634634
// Instantiate.
635635
if (!(Activator.CreateInstance(type) is InputBindingComposite instance))
636-
throw new Exception(
636+
throw new InvalidOperationException(
637637
$"Registered type '{type.Name}' used for '{nameAndParametersParsed.name}' is not an InputBindingComposite");
638638

639639
// Set parameters.
@@ -653,15 +653,15 @@ private static int AssignCompositePartIndex(object composite, string name, ref i
653653
var field = type.GetField(name,
654654
BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
655655
if (field == null)
656-
throw new Exception(
656+
throw new InvalidOperationException(
657657
$"Cannot find public field '{name}' used as parameter of binding composite '{composite}' of type '{type}'");
658658

659659
////REVIEW: should we wrap part numbers in a struct instead of using int?
660660

661661
// Type-check.
662662
var fieldType = field.FieldType;
663663
if (fieldType != typeof(int))
664-
throw new Exception(
664+
throw new InvalidOperationException(
665665
$"Field '{name}' used as a parameter of binding composite '{composite}' must be of type 'int' but is of type '{type.Name}' instead");
666666

667667
////REVIEW: this create garbage; need a better solution to get to zero garbage during re-resolving

Packages/com.unity.inputsystem/InputSystem/Controls/InputControl.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -877,8 +877,8 @@ internal TProcessor TryGetProcessor<TProcessor>()
877877
internal override void AddProcessor(object processor)
878878
{
879879
if (!(processor is InputProcessor<TValue> processorOfType))
880-
throw new Exception(
881-
$"Cannot add processor of type '{processor.GetType().Name}' to control of type '{GetType().Name}'");
880+
throw new ArgumentException(
881+
$"Cannot add processor of type '{processor.GetType().Name}' to control of type '{GetType().Name}'", nameof(processor));
882882
m_ProcessorStack.Append(processorOfType);
883883
}
884884

Packages/com.unity.inputsystem/InputSystem/Controls/InputControlLayout.cs

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Reflection;
55
using System.Runtime.CompilerServices;
66
using System.Runtime.InteropServices;
7+
using System.Runtime.Serialization;
78
using UnityEngine.InputSystem.LowLevel;
89
using UnityEngine.InputSystem.Utilities;
910

@@ -1175,7 +1176,7 @@ private static void ThrowIfControlItemIsDuplicate(ref ControlItem controlItem,
11751176
foreach (var existing in controlLayouts)
11761177
if (string.Compare(name, existing.name, StringComparison.OrdinalIgnoreCase) == 0 &&
11771178
existing.variants == controlItem.variants)
1178-
throw new Exception($"Duplicate control '{name}' in layout '{layoutName}'");
1179+
throw new InvalidOperationException($"Duplicate control '{name}' in layout '{layoutName}'");
11791180
}
11801181

11811182
internal static void ParseHeaderFieldsFromJson(string json, out InternedString name,
@@ -1246,7 +1247,7 @@ public InputControlLayout ToLayout()
12461247
}
12471248
else if (!typeof(InputControl).IsAssignableFrom(type))
12481249
{
1249-
throw new Exception($"'{this.type}' used by layout '{name}' is not an InputControl");
1250+
throw new InvalidOperationException($"'{this.type}' used by layout '{name}' is not an InputControl");
12501251
}
12511252
}
12521253
else if (string.IsNullOrEmpty(extend))
@@ -1280,7 +1281,7 @@ public InputControlLayout ToLayout()
12801281
else if (beforeRenderLowerCase == "update")
12811282
layout.m_UpdateBeforeRender = true;
12821283
else
1283-
throw new Exception($"Invalid beforeRender setting '{beforeRender}'");
1284+
throw new InvalidOperationException($"Invalid beforeRender setting '{beforeRender}'");
12841285
}
12851286

12861287
// Add common usages.
@@ -1294,7 +1295,7 @@ public InputControlLayout ToLayout()
12941295
foreach (var control in controls)
12951296
{
12961297
if (string.IsNullOrEmpty(control.name))
1297-
throw new Exception($"Control with no name in layout '{name}");
1298+
throw new InvalidOperationException($"Control with no name in layout '{name}");
12981299
var controlLayout = control.ToLayout();
12991300
ThrowIfControlItemIsDuplicate(ref controlLayout, controlLayouts, layout.name);
13001301
controlLayouts.Add(controlLayout);
@@ -1561,9 +1562,9 @@ private InputControlLayout TryLoadLayoutInternal(InternedString name)
15611562
{
15621563
var layoutObject = builder.method.Invoke(builder.instance, null);
15631564
if (layoutObject == null)
1564-
throw new Exception($"Layout builder '{name}' returned null when invoked");
1565+
throw new InvalidOperationException($"Layout builder '{name}' returned null when invoked");
15651566
if (!(layoutObject is InputControlLayout layout))
1566-
throw new Exception(
1567+
throw new InvalidOperationException(
15671568
$"Layout builder '{name}' returned '{layoutObject}' which is not an InputControlLayout");
15681569
return layout;
15691570
}
@@ -1712,14 +1713,35 @@ internal struct BuilderInfo
17121713
public object instance;
17131714
}
17141715

1715-
internal class LayoutNotFoundException : Exception
1716+
public class LayoutNotFoundException : Exception
17161717
{
17171718
public string layout { get; }
1718-
public LayoutNotFoundException(string name, string message = null)
1719-
: base(message ?? $"Cannot find control layout '{name}'")
1719+
1720+
public LayoutNotFoundException()
1721+
{
1722+
}
1723+
1724+
public LayoutNotFoundException(string name, string message)
1725+
: base(message)
1726+
{
1727+
layout = name;
1728+
}
1729+
1730+
public LayoutNotFoundException(string name)
1731+
: base($"Cannot find control layout '{name}'")
17201732
{
17211733
layout = name;
17221734
}
1735+
1736+
public LayoutNotFoundException(string message, Exception innerException) :
1737+
base(message, innerException)
1738+
{
1739+
}
1740+
1741+
protected LayoutNotFoundException(SerializationInfo info,
1742+
StreamingContext context) : base(info, context)
1743+
{
1744+
}
17231745
}
17241746

17251747
// Constructs InputControlLayout instances and caches them.

Packages/com.unity.inputsystem/InputSystem/Controls/InputControlList.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ public void AddSlice<TList>(TList list, int count = -1, int destinationIndex = -
179179
if (count == 0)
180180
return;
181181
if (sourceIndex + count > list.Count)
182-
throw new ArgumentOutOfRangeException(
183-
$"Count of {count} elements starting at index {sourceIndex} exceeds length of list of {list.Count}", "count");
182+
throw new ArgumentOutOfRangeException(nameof(count),
183+
$"Count of {count} elements starting at index {sourceIndex} exceeds length of list of {list.Count}");
184184

185185
// Make space in the list.
186186
if (Capacity < count)

0 commit comments

Comments
 (0)