The ArgumentException class in the System namespace of C# is used to represent an exception that is thrown when one of the arguments provided to a method is not valid. This class derives from the SystemException class, which makes it a part of the core .NET Framework.
Code Examples:
Example 1:
In this example, we create an instance of the ArgumentException class and customize its error message by setting the Message property. We then call the ToString() method on the instance to get a string representation of the exception.
ArgumentException ex = new ArgumentException("The argument is invalid."); string errorMessage = ex.ToString();
Example 2:
In this example, we create a method that accepts two arguments. Inside the method, we check if the arguments satisfy a particular condition. If they don't, we throw an instance of the ArgumentException class. We then catch the exception and use its ToString() method to log the error.
using System;
public void Process(int arg1, string arg2) { if (arg1 <= 0 || string.IsNullOrEmpty(arg2)) { throw new ArgumentException("One or both arguments are invalid."); }
This class is a part of the core .NET Framework, which means it is available in the System namespace by default and doesn't require any additional package or library to be installed.
C# (CSharp) System ArgumentException.ToString - 29 examples found. These are the top rated real world C# (CSharp) examples of System.ArgumentException.ToString extracted from open source projects. You can rate examples to help us improve the quality of examples.