Voici un code d'exemple, où on a volontairement fait appel (ligne 25) à la liste maListe qui n'a pas été initialisée (ligne 19) :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace testException
{
public partial class Fille : Form
{
public Fille()
{
InitializeComponent();
}
List<int> maListe;
private void Fille_Load(object sender, EventArgs e)
{
try
{
foreach (int i in maListe)
{
}
}
catch (NullReferenceException) { Text = "Exception"; return; }
Text = "Completed";
}
}
}
Pour corriger ce comportement par défaut, il suffit de se rendre dans le menu "Déboguer > Exceptions..." et de cocher la case System.NullReferenceException.
![]() |
| Cochez la case System.NullReferenceException |
![]() |
| Le débogueur de VS 2010 à capturé NullReferenceException |

