我利用C#创建了一个简单的假释申请。 它创造了一个阶级的例子,并称之为一种阶级方法。 这种方法包含一个捕获系统例外情况的试捕区。 Net.WebException and rethrow it so that the main methods canrip it and act appropriate. 当我执行所汇编的申请时,该例外没有传递给主要类别,使用者永远不会看到我的习惯错误信息。 相反,这种屏幕唤醒了我,告诉我,存在着一个不受约束的网络外观(它用德语进行,但我认为它可以承认任何途径;-)
alt text http://img29.imageshack.us/img29/4581/crapq.png
这是我称之为“Obrouter”的班子内的方法:
public void Login()
{
string sUrl = _sUrl + "/somestuff.htm";
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(sUrl);
CredentialCache credCache = new CredentialCache();
credCache.Add(new Uri(sUrl), "Basic", _credential);
request.Credentials = credCache;
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream receiveStream = response.GetResponseStream();
StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
_sContent = reader.ReadToEnd();
response.Close();
receiveStream.Dispose();
reader.Dispose();
_parseSessionIds();
}
catch (WebException)
{
throw;
}
}
这是我的主类中的方法:
private static bool _login()
{
_router = new BuffaloRouter(_sIP, new System.Net.NetworkCredential("root", _sPassword));
try
{
Console.WriteLine("Login...");
_router.Login();
return true;
}
catch (System.Net.WebException)
{
_showErrorMessage("Could not connect to " + _sIP);
return false;
}
}
UPDATE: I feel more than a little embarrassed and would rather not talk about it. But like a few times before I didn t relly look at what I was doing ;-) The method inside the main class was not even invoked when I was running the app. The one that was invoked didn t have a try-catch block so that the exception thrown inside my class method made the app do what it was supposed to, i.e. CRASH!! I m stupid, sorry for wasting everone s time.