来个代码说明问题: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掉了。。
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掉了。。
{
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);
} }
for(.....)
{
if(i==0)
{
continu;
}
.....
}
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);
}
}
}
}
}
{
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);
} }
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); }
}
}
}
2.在会发生异常的代码前加一个if判断,如果i=0则跳过
VB.NET编译器会给每条语句添加Goto,忽略异常。C# 里面至今没有等效好用的语句。