C# exception
main program
public static void ValidateAge(int age)
{
if (age < 0)
throw new ArgumentOutOfRangeException(nameof(age), "L'âge ne peut pas être négatif.");
}
public static void test1()
{
try
{
ValidateAge(-1);
}
catch (Exception ex)
{
Console.WriteLine($"{ex.Message}\n{ex}");
}
finally
{
Console.WriteLine("out");
}
}
console
L'âge ne peut pas être négatif. (Parameter 'age')
System.ArgumentOutOfRangeException: L'âge ne peut pas être négatif. (Parameter 'age')
at TutoException.CException.ValidateAge(Int32 age) in D:\JACK2\DEV\CSharp\MAINCS\CSHARP\TutoException\CException.cs:line 47
at TutoException.CException.test1() in D:\JACK2\DEV\CSharp\MAINCS\CSHARP\TutoException\CException.cs:line 55
out
custom exception
public CustomException(string message) : base(message)
{
Console.WriteLine("Class custom");
}
public static void test2()
{
try
{
throw new CustomException("Erreur personnalisée !");
}
catch (CustomException ex)
{
Console.WriteLine($"Exception custom : {ex.Message}");
}
}
console
Class custom
Exception custom : Erreur personnalisée !