我想抓住用户输入无效的时间以及他们没有输入的时间。我只能捕获无效的输入。
string filename;
Bitmap picture;
Console.Write("Write the file you want to edit; ")
try
{
filename = Console.ReadLine();
picture = new Bitmap(filename);
}
catch (ArgumentNullException)
{
Console.WriteLine("No file choosen");
Console.ReadLine();
return;
}
catch (ArgumentException)
{
Console.WriteLine("File not valid");
Console.ReadLine();
return;
答案 0 :(得分:1)
我将使用IsNullOrEmpty()
,例如:
if (string.IsNullOrEmpty(filename)
{
// throw an error
}
// otherwise, continue