Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
ArgumentNullException in C#
The ArgumentNullException is thrown when a null reference is passed to a method that does not accept it as a valid argument. This exception is part of the System namespace and helps prevent null reference errors by catching them early.
This exception commonly occurs when methods expect non-null parameters but receive null values instead. It provides clear error messages indicating which parameter was null.
Syntax
Following is the syntax for throwing ArgumentNullException −
throw new ArgumentNullException(paramName); throw new ArgumentNullException(paramName, "Custom message");
Following is the syntax for handling ArgumentNullException −
try {
// code that might throw ArgumentNullException
}
catch (ArgumentNullException ex) {
// handle the exception
}
Common Scenarios Causing ArgumentNullException
Example 1: Using int.Parse() with Null Value
When we pass a null parameter to int.Parse() method, ArgumentNullException is thrown −
using System;
class Demo {
static void Main() {
try {
string val = null;
int res = int.Parse(val);
Console.WriteLine("Result: " + res);
}
catch (ArgumentNullException ex) {
Console.WriteLine("ArgumentNullException caught: " + ex.Message);
Console.WriteLine("Parameter name: " + ex.ParamName);
}
}
}
The output of the above code is −
ArgumentNullException caught: Value cannot be null. Parameter name: s
Example 2: String Methods with Null Parameters
Many string methods throw ArgumentNullException when null values are passed −
using System;
class Program {
static void Main() {
try {
string str1 = null;
string str2 = "Hello";
// This will throw ArgumentNullException
bool result = str2.Equals(str1); // This won't throw
Console.WriteLine("Equals result: " + result);
// This will throw ArgumentNullException
string combined = string.Concat(str1, str2);
}
catch (ArgumentNullException ex) {
Console.WriteLine("Exception: " + ex.GetType().Name);
Console.WriteLine("Message: " + ex.Message);
}
}
}
The output of the above code is −
Equals result: False Exception: ArgumentNullException Message: Value cannot be null.
Preventing ArgumentNullException
Example 3: Using Null Checks
You can prevent ArgumentNullException by checking for null values before using them −
using System;
class SafeExample {
static void ProcessString(string input) {
if (input == null) {
Console.WriteLine("Input is null, cannot process");
return;
}
Console.WriteLine("Processing: " + input.ToUpper());
}
static void Main() {
string validString = "Hello World";
string nullString = null;
ProcessString(validString);
ProcessString(nullString);
// Safe parsing with TryParse
if (int.TryParse(nullString, out int result)) {
Console.WriteLine("Parsed value: " + result);
} else {
Console.WriteLine("Could not parse the value");
}
}
}
The output of the above code is −
Processing: HELLO WORLD Input is null, cannot process Could not parse the value
ArgumentNullException Properties
| Property | Description |
|---|---|
| Message | Gets the error message that describes the exception |
| ParamName | Gets the name of the parameter that caused the exception |
| StackTrace | Gets the stack trace showing where the exception occurred |
Conclusion
ArgumentNullException is a crucial exception in C# that prevents null reference errors by catching them early when null values are passed to methods that don't accept them. Always validate input parameters and use try-catch blocks or null checks to handle potential null values gracefully.
