来个代码说明问题:using System;
using System.Collections.Generic;
using System.Text;
using System.Net;namespace Ex
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            p.test();
            Console.Read();
        }        public void test()
        {
            try
            {
                for (int i = 0; i < 2; i++)
                {
                    Console.WriteLine(i + ":" + 0);                    // 一下这2句话会发生异常
                    HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost:8088/a.jsp");
                    HttpWebResponse res = (HttpWebResponse)req.GetResponse();                    Console.WriteLine(i + ":" + 1);
                    Console.WriteLine(i + ":" + 2);
                }
            }
            catch (Exception e)
            {                Console.WriteLine(e.Message);
            }
            
        }
    }
}
在i=0的时候发生异常,因为这个请求的page是我故意写的不存在的页面,在i=0的时候会发生异常,但是我想让i有=1的处理机会,我怎么在catch中写代码呢?这个和java中完全不一样,java中发生异常,进入catch快,然后接着往下执行,c#中for代码发生异常,直接break掉了。。

解决方案 »

  1.   

    for (int i = 0; i < 2; i++)
                    {
                try
                {                    Console.WriteLine(i + ":" + 0);                    // 一下这2句话会发生异常
                        HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost:8088/a.jsp");
                        HttpWebResponse res = (HttpWebResponse)req.GetResponse();                    Console.WriteLine(i + ":" + 1);
                        Console.WriteLine(i + ":" + 2);
    } catch (Exception e)
                {                Console.WriteLine(e.Message);
                }                }
      

  2.   

    for循环里面用if判断下就好了撒
    for(.....)
    {
       if(i==0)
       {
           continu;
       }
       .....
    }
      

  3.   


    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net;namespace Ex
    {
        class Program
        {
            static void Main(string[] args)
            {
                Program p = new Program();
                p.test();
                Console.Read();
            }        public void test()
            {
                for (int i = 0; i < 2; i++)
                {
                    Console.WriteLine(i + ":" + 0);
                    try
                    {  
                        // 一下这2句话会发生异常
                        HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost:8088/a.jsp");
                        HttpWebResponse res = (HttpWebResponse)req.GetResponse();                    Console.WriteLine(i + ":" + 1);
                        Console.WriteLine(i + ":" + 2);
                    }            
                    catch (Exception e)
                    {                    Console.WriteLine(e.Message);
                    }
                }            
            }
        }
    }
      

  4.   

     public void test()
            {
                try
                {
                    for (int i = 0; i < 2; i++)
                    {
                        Console.WriteLine(i + ":" + 0);                    // 一下这2句话会发生异常
                        try
                        {
                            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost:8088/a.jsp");
                            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
                        }
                        catch
                        {
                            continue;
                        }
                        Console.WriteLine(i + ":" + 1);
                        Console.WriteLine(i + ":" + 2);
                    }
                }
                catch (Exception e)
                {                Console.WriteLine(e.Message);
                }        }
      

  5.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net;namespace Ex
    {
        class Program
        {
            static void Main(string[] args)
            {
                Program p = new Program();
                p.test();
                Console.Read();
            }        public void test()
            {
                for (int i = 0; i < 2; i++)
                {
                    Console.WriteLine(i + ":" + 0);
                    try
                    {  
                        // 一下这2句话会发生异常
                        HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost:8088/a.jsp");
                        HttpWebResponse res = (HttpWebResponse)req.GetResponse();
                    }            
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }                Console.WriteLine(i + ":" + 1);
                    Console.WriteLine(i + ":" + 2);            }            
            }
        }
    }
      

  6.   

    1.在for循环中try,出现异常catch后,会继续往下走
    2.在会发生异常的代码前加一个if判断,如果i=0则跳过
      

  7.   

    用VB.NETOn Error Resume Next
    VB.NET编译器会给每条语句添加Goto,忽略异常。C# 里面至今没有等效好用的语句。