VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > C#教程 >
  • C#语法学习异常处理exception

制作者:剑锋冷月 单位:无忧统计网,www.51stat.net
 

usingSystem;
classTest
{
  staticvoidMain()
  {
    /*
    try
    {
      intn=10;
      intm=0;
      floatf=n/m;
    }
    catch(Exceptione)
    {
      Console.WriteLine(e.Message);
    }
    finally
    {
      Console.WriteLine("finally");
    }
    Console.WriteLine("finallylater");
    //*/
    
    /*
    try
    {
      intn=10;
      intm=0;
      floatf=n/m;
    }
    catch(Exceptione)
    {
      Console.WriteLine("ERROR");
    }
    finally
    {
      Console.WriteLine("finally");
    }
    Console.WriteLine("finallylater");
    //*/
    
    /*
    try
    {
      intn=10;
      intm=0;
      floatf=n/m;
    }
    //這里應該把庇配度高的異常放到前面,依次是庇配度越低的
    //自然Exception也就放在最後面.
    catch(DivideByZeroExceptione)
    {
      Console.WriteLine(e.Message);
    }
    catch(Exceptione)
    {
      Console.WriteLine(e.Message);
    }
    finally
    {
      Console.WriteLine("finally");
    }
    Console.WriteLine("finallylater");
    //*/    
    
    /*
    try
    {
      intn=10;
      intm=0;
      floatf=n/m;
    }
    //此處catch後面沒有跟(Exceptione)
    //程序會認為是catch(Exceptione)
    catch
    {
      Console.WriteLine("ERROR");
    }
    finally
    {
      Console.WriteLine("finally");
    }
    Console.WriteLine("finallylater");
    //*/
    
    /*
    try
    {
      strings=null;
      if(s==null)
      {
        thrownewArgumentNullException();
      }
    }
    catch
    {
  
      //這里拋出一個異常
      Console.WriteLine("接收到拋出的異常");
    }
    finally
    {
      Console.WriteLine("finally");
    }
    Console.WriteLine("finallylater");
    //*/
    
  }
}

 

/*
*CreatedbySharpDevelop.
*User:Administrator
*Date:2008/9/11
*Time:下午04:16
*
*/
usingSystem;
usingSystem.IO;
publicclassExceptions
{
  publicstaticintMain()
  {
    byte[]myStream=newbyte[3];
    StreamWritersw=newStreamWriter("exceptions.txt");
    try
    {
      for(byteb=0;b<10;b++)
      {
        sw.WriteLine("Byte{0}:{1}",b+1,b);
        myStream[b]=b;
      }
    }
    catch(Exceptione)
    {
      Console.WriteLine(e.Message);
    }
    finally
    {
      sw.WriteLine("Close");
      sw.Close();
    }
    return0;
  }
}

/*
*CreatedbySharpDevelop.
*User:Administrator
*Date:2008/9/11
*Time:下午04:29
*
*/
usingSystem;
usingSystem.IO;
publicclassTooManyItemsException:Exception
{
  publicTooManyItemsException():base(@"自己設計的異常信息"){}
}
publicclassExceptionTester
{
  publicstaticintMain()
  {
    ExceptionTestermyExceptionMaker=newExceptionTester();
    try
    {
      myExceptionMaker.GenerateException(5);
    }
    catch(Exceptione)
    {
      Console.WriteLine(e.Message);
    }
    finally
    {
      Console.WriteLine("FinallyfromMain.");
    }
    return0;
  }
  voidGenerateException(intiterations)
  {
    intmySize=3;
    byte[]myStream=newbyte[mySize];
    StreamWritersw=newStreamWriter("aa.txt");
    try
    {
      if(iterations>myStream.Length)
      {
        thrownewTooManyItemsException();
      }
      for(byteb=0;b<iterations;b++)
      {
        sw.WriteLine("Byte{0}:{1}",b+1,b);
        myStream[b]=b;
      }
    }
    finally
    {
      Console.WriteLine("FinallyfromGenerateException.");
      sw.WriteLine("Close");
      sw.Close();
    }
  }
}

 

/*
*CreatedbySharpDevelop.
*User:Administrator
*Date:2008/9/11
*Time:下午04:16
*
*/
usingSystem;
usingSystem.IO;
publicclassExceptions
{
  publicstaticintMain()
  {
    byte[]myStream=newbyte[3];
    StreamWritersw=newStreamWriter("exceptions.txt");
    try
    {
      for(byteb=0;b<10;b++)
      {
        sw.WriteLine("Byte{0}:{1}",b+1,b);
        myStream[b]=b;
      }
    }
    catch(Exceptione)
    {
      Console.WriteLine(e.Message);
    }
    finally
    {
      sw.WriteLine("Close");
      sw.Close();
    }
    return0;
  }
}

/*
*CreatedbySharpDevelop.
*User:Administrator
*Date:2008/9/11
*Time:下午04:29
*
*/
usingSystem;
usingSystem.IO;
publicclassTooManyItemsException:Exception
{
  publicTooManyItemsException():base(@"自己設計的異常信息"){}
}
publicclassExceptionTester
{
  publicstaticintMain()
  {
    ExceptionTestermyExceptionMaker=newExceptionTester();
    try
    {
      myExceptionMaker.GenerateException(5);
    }
    catch(Exceptione)
    {
      Console.WriteLine(e.Message);
    }
    finally
    {
      Console.WriteLine("FinallyfromMain.");
    }
    return0;
  }
  voidGenerateException(intiterations)
  {
    intmySize=3;
    byte[]myStream=newbyte[mySize];
    StreamWritersw=newStreamWriter("aa.txt");
    try
    {
      if(iterations>myStream.Length)
      {
        thrownewTooManyItemsException();
      }
      for(byteb=0;b<iterations;b++)
      {
        sw.WriteLine("Byte{0}:{1}",b+1,b);
        myStream[b]=b;
      }
    }
    finally
    {
      Console.WriteLine("FinallyfromGenerateException.");
      sw.WriteLine("Close");
      sw.Close();
    }
  }
}



相关教程