CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Exceptions

  1. #1
    Join Date
    May 2006
    Posts
    10

    Exceptions

    Is there a way to get the parameters and their values from a method that threw an exception. I can't find anywhere in the exception object that tells you this ...it only says targetsite, but not it's parameters and their values..

    Thanks!!

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Exceptions

    Hello jm1234!

    I'm not sure what you mean, could you please elaborate ¿

    You might find this FAQ on Exceptions, quite handy :
    http://www.codeguru.com/forum/showthread.php?t=383057

  3. #3
    Join Date
    Dec 2002
    Posts
    305

    Re: Exceptions

    Within the method, put a try.. catch.. end try loop.

    Example of an exception reported from within a method:

    Private sub TestException()
    Dim z as single, x as string
    Try
    x="A"
    z=csng(x)*csng(x)
    catch
    msgbox(x)
    end try
    end sub

    Because x is a string value that does not translate to a floating point value. It throws an exception when z is evaluated, which is caught and reported.

    Hope this is what you are looking for.

  4. #4
    Join Date
    May 2006
    Posts
    10

    Re: Exceptions

    Say on my asp.net page, I call a method "Foo". ...but Foo calls another method "FooSub" in itself (that takes two parameters, x and y), which causes an throws an exception for some reason. This exception isn't caught until we get back to the page level, in which case the page redirects to an error page, in which an admin level person viewing the page can see the exception's data. Right here, I want to display that it was FooSub that threw the error and I also want to display what FooSub's parameters were (x and y) and what the values passed in for them were. There doesn't seem to be a way to do that. I know that exception.TargetSite will get me my method that threw it (in this case, SubFoo), but what property/method in exception will tell me what SubFoo's parameter and parameter values were?

  5. #5
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868

    Re: Exceptions

    You'll have to create your own exception class and put the values in manually.

  6. #6
    Join Date
    May 2006
    Posts
    10

    Re: Exceptions

    I suppose I could getthecurrentmethod using system.reflection.memberinfo.getcurrentmethod, and then call getparameters to get each parameter for the method. ...There's only one problem with this though ...it gets the parameters, but doesn't get the current value of the parameter. It seems you should be able to get this somehow with reflection ...if you can get the current values of properties within an object, then why can't you get the current value of the parameters passed to a current method? Is there a way to do this?
    Thanks!!

  7. #7
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868

    Re: Exceptions

    Don't overlook the StackTrace class either, but it doesn't include values either.

  8. #8
    Join Date
    Jan 2006
    Posts
    293

    Re: Exceptions

    DSJ is right. The StackTrace should provide this information. It should have the line listed in the page with the try/catch block, and higher up in the stack it (when displayed), it should also have an error listed in the method that is in the other place, with a line number as well...
    Code:
            'code in form
            Try
                DoAnError()
            Catch ex As Exception
                MessageBox.Show(ex.StackTrace)
            End Try
    
            'code in module
            Public Function DoAnError() As Boolean
                 'file that doesnt exist, throws error
                 Dim MyWriter As New IO.StreamReader("c:\nofile.txt")
            End Function
    And an example of the stacktrace:
    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
    at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
    at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize)
    at System.IO.StreamReader..ctor(String path)
    at TestApp.Module1.DoAnError() in C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\TestApp\TestApp\Module1.vb:line 12
    at TestApp.Form1.Button1_Click(Object sender, EventArgs e) in C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\TestApp\TestApp\Form1.vb:line 5
    If you notice, the last two lines show the form where the error occurred, and before that it shows the procedure located in the module, with a line number as well...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured