我有 c# 程序,这个 Get Sql Query From Input 并执行这个。有时,此查询包含执行查询后返回“除零异常”的任何单词。
我想在执行查询之前禁用SqlException 并检查其他输入的单词,然后启用它。
您正在寻找的功能称为Try...Catch。
在 MSDN 上阅读它。
当他不寻找 try and catch 时,他可能正在寻找#pragma 来禁用异常
很多时候,根本不需要例外。
例如,如果我打开一个文件并使用错误的名称,或者它的繁忙或其他什么,
操作的返回为空。这对我来说是无用的,我要求了一些东西,但它没有用。
但是不,我必须用各种有趣的业务来围绕我的代码。
StreamReader streamReader=new StreamReader(filename);
if (streamReader==null)
Console.WriteLine("File Not Ready To Be Read {0}!",filename)
else
{
//go on with your business
}
//与这个编译的对比只是因为我不得不什么都不做,什么也不做,最后做我所关注的事情。使用系统;使用 System.IO;
class ExceptionHandling
{
public static void Main(string[] args)
{
StreamReader streamReader = null;
try
{
streamReader = new StreamReader("C:\\bin\\C#\\readfil1e\\data.txt");//or @"c:\bin\c#\readfile\data.txt");
}
catch(Exception ex)
{
}
finally
{
if (streamReader == null)
{
Console.WriteLine("FILE NOT READABLE");
Console.ReadLine();
}
else
{
Console.WriteLine(streamReader.ReadToEnd());
Console.ReadLine();
streamReader.Close();
}
}//finally
}
}