Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

FileNotFoundException qui ne se déclenche pas.

1 réponse
Avatar
Jean-Luc M.
Bonjour,

Suis tout nouveau débutant en C#.
Je suis les exemples d'un bouquin et j'ai un souci avec un exception.

try
{
FileInfo fil = new FileInfo("trucmachinchose.txt");
if (fil.Exists)
{
MessageBox.Show(fil.Extension);
}
}
catch FileNotFoundException ex)
{
MessageBox.Show(ex.ToString());
}


Le fichier trucmachinchose.txt n'existe pas et pourtant le code ne
passe jamais dans le catch. Je pensais que que la ligne
FileInfo fil = new FileInfo("trucmachinchose.txt");
déclencherait une exception mais en fait non.

Est-ce un comportement normal ou faut-il ajouter quelquechose au code ?

J-L



--
J-L M. (Alphomega)
ICQ: 149635116
Pour m'écrire, cliquer le lien ci-dessous
http://cerbermail.com/?G5iYdBb2Ce

1 réponse

Avatar
Christophe Lephay
"Jean-Luc M." a écrit dans le message de news:

try
{
FileInfo fil = new FileInfo("trucmachinchose.txt");
if (fil.Exists)
{
MessageBox.Show(fil.Extension);
}
}
catch FileNotFoundException ex)
{
MessageBox.Show(ex.ToString());
}


Le fichier trucmachinchose.txt n'existe pas et pourtant le code ne passe
jamais dans le catch. Je pensais que que la ligne
FileInfo fil = new FileInfo("trucmachinchose.txt");
déclencherait une exception mais en fait non.

Est-ce un comportement normal ou faut-il ajouter quelquechose au code ?



C'est normal. En l'espèce, la propriété Exists n'aurait pas de sens si une
exception était lancée lorsque le fichier n'existe pas.

Les exceptions susceptibles d'être lancées par le constructeur se trouvent
ici
http://msdn2.microsoft.com/fr-fr/library/system.io.fileinfo.fileinfo(VS.80).aspx
et FileNotFoundException n'y figure pas.