English 中文(简体)
。 互联网例外从班级转至主要课程
原标题:.NET exception doesn t get passed from class to main program

我利用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.

问题回答

如果你再次这样做的话,那么你就不必首先抓住这个例外情况。 仅从<代码><<<0> >代码>方法上删除<代码>try.>fish>。

这样做时,暂时搁置了你的法典,以赶上一般性例外,并欺骗其寻找实际提出的例外。 这样做后,该法典又重新赶上这一例外(仅此例外)。

As Chris F已经指出,没有必要重新计算<代码><<<0>>代码>方法中的错误。 如果贵国的捕获区没有在_login方法中运行,那么我会猜测你没有达到适当的例外。

您获得<代码>System.Net.WebException





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签