Click here to Skip to main content
15,902,492 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
GeneralRe: [vb.net 2008] How get mp3 title,author and lenght ? Pin
nRush11-Mar-10 23:30
nRush11-Mar-10 23:30 
GeneralRe: [vb.net 2008] How get mp3 title,author and lenght ? Pin
Richard MacCutchan12-Mar-10 1:07
mveRichard MacCutchan12-Mar-10 1:07 
GeneralRe: [vb.net 2008] How get mp3 title,author and lenght ? Pin
nRush12-Mar-10 2:54
nRush12-Mar-10 2:54 
QuestionCitrix & WebEx Wrestle Microsoft Might in the Collaboration Tools Category at Developer Summit Award(developersummit.com/gida3_llist) Pin
Shaguf Mohtisham11-Mar-10 20:15
Shaguf Mohtisham11-Mar-10 20:15 
QuestionHow to get local variables and object instance that threw unhandled exception Pin
dybs11-Mar-10 18:42
dybs11-Mar-10 18:42 
AnswerRe: How to get local variables and object instance that threw unhandled exception Pin
Eddy Vluggen12-Mar-10 0:56
professionalEddy Vluggen12-Mar-10 0:56 
GeneralRe: How to get local variables and object instance that threw unhandled exception Pin
dybs12-Mar-10 2:27
dybs12-Mar-10 2:27 
AnswerRe: How to get local variables and object instance that threw unhandled exception [long answer] Pin
Eddy Vluggen12-Mar-10 5:45
professionalEddy Vluggen12-Mar-10 5:45 
dybs wrote:
my goal is to get as much info as I can for unhandled exceptions


Ditto, and you're right - if the information contained helps with the debugging-proces. There's no easy anwer to your question, once one says that it's technically possible one has to answer whether it's worth the effort.


dybs wrote:
In my case that won't really work because I need this information in the UnhandledException handler, and by the time I get to this point (the first time I actually have an exception to look at), I no longer have access to the object that threw the exception.


I have created a small console-application to test with;
C#
class Program
{
    static Form myForm = new Form();
    static Button myButton = new Button();
	
    [STAThread]
    public static void Main(string[] args)
    {												
        myButton.Click += delegate 
        {
            new MyTextCache().DoLoadStuff();
        };
        myForm.Controls.Add(myButton);
		
        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(logEx);
        Application.Run(myForm);
    }

    static void logEx(object sender, UnhandledExceptionEventArgs e)
    {
        Exception ex = (Exception)e.ExceptionObject;
        Console.WriteLine("Exception: " + e.ExceptionObject.GetType().ToString());
        if (ex.Data.Count > 0)
        {
            foreach(var item in ex.Data)
            {
                var entry = (System.Collections.DictionaryEntry)item;
                Console.WriteLine("Parameter: " + entry.Value.ToString());
            }
        }
    }
}
Rather simple, just creates a form with a button and logs any exceptions on the console. The key here is looping the Data property. We know that we can fill it when we throw an exception ourselves. Now, imagine that there's an unexpected exception thrown by a .NET class in the MyTextCache.DoLoadStuff method. To add the info, we'd need to catch it, attach the desired information, and rethrow it.

The MyTextCache could be adapted like shown below. There's a local exception-handler that doesn't handle the FileNotFound-exception, but instead adds a reference to the object;
C#
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;

[Serializable()]
class MyTextCache: ISerializable
{
    #region variables that represent Internal State
    string _filename;
    string _contents;
    #endregion
	
    public MyTextCache()
    {
        _filename = "Q:\\Hello World.txt";
    }
	
    public MyTextCache(SerializationInfo info, StreamingContext ctxt)
    {
        _filename = (String)info.GetValue("filename", typeof(string));
        _contents = (String)info.GetValue("contents", typeof(string));
    }
    
    public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
    {
        info.AddValue("filename", _filename);
        info.AddValue("contents", _contents);
    }
	
    public override string ToString()
    {
        return string.Format("Filename: {0}\nContents: {1}\n",
            _filename, 
            _contents);
    }
	
    internal void DoLoadStuff()
    {
        try
        {
            _contents = System.IO.File.ReadAllText(_filename);
        }
        // for every exception we didn't expect;
        catch(Exception e) 
        {				
            // Add the plaintiff
            e.Data.Add("the object: ", this);

            // Don't handle now, rethrow
            throw e;
        }
    }		
}
That would give you access to the _filename and _contents properties of the MyTextCache class. You should see an invalid filename, and an empty contents-property on the console if you attempt this.

There's a second problem; you can log the contents of the class now (or serialize it), but that doesn't give you access to the local variables from the DoLoadStuff method. This can be solved by modifying the calling method, like thus;
C#
internal void DoLoadStuff()
{
    int i = 9; // Number of attempts or somethin' the like
    try
    {
        _contents = System.IO.File.ReadAllText(_filename);
    }
    catch(Exception e)
    {				
        e.Data.Add("the object: ", this);
        e.Data.Add("local i: ", i);
        throw e;
    }
}
As you can see, it would be a lot of work to add "all" local variables for "each" method in the project. Adding a reference to the originating class might be worth the time to modify some of the key-classes.
I are Troll Suspicious | :suss:

GeneralRe: How to get local variables and object instance that threw unhandled exception [long answer] Pin
dybs12-Mar-10 7:24
dybs12-Mar-10 7:24 
GeneralRe: How to get local variables and object instance that threw unhandled exception [long answer] Pin
Eddy Vluggen12-Mar-10 7:40
professionalEddy Vluggen12-Mar-10 7:40 
QuestionManipulate bitmap in unmanaged c++ DLL from a VB.NET app Pin
frito_burrito11-Mar-10 11:42
frito_burrito11-Mar-10 11:42 
AnswerRe: Manipulate bitmap in unmanaged c++ DLL from a VB.NET app Pin
Luc Pattyn11-Mar-10 12:02
sitebuilderLuc Pattyn11-Mar-10 12:02 
GeneralRe: Manipulate bitmap in unmanaged c++ DLL from a VB.NET app Pin
frito_burrito12-Mar-10 4:36
frito_burrito12-Mar-10 4:36 
GeneralRe: Manipulate bitmap in unmanaged c++ DLL from a VB.NET app Pin
Luc Pattyn12-Mar-10 4:52
sitebuilderLuc Pattyn12-Mar-10 4:52 
GeneralRe: Manipulate bitmap in unmanaged c++ DLL from a VB.NET app Pin
frito_burrito15-Mar-10 4:26
frito_burrito15-Mar-10 4:26 
QuestionSerialization of collection Pin
johnjitu11-Mar-10 4:04
johnjitu11-Mar-10 4:04 
AnswerRe: Serialization of collection Pin
The Man from U.N.C.L.E.11-Mar-10 7:49
The Man from U.N.C.L.E.11-Mar-10 7:49 
GeneralRe: Serialization of collection Pin
johnjitu11-Mar-10 20:20
johnjitu11-Mar-10 20:20 
QuestionSerialization of colleciton Pin
johnjitu11-Mar-10 0:19
johnjitu11-Mar-10 0:19 
AnswerRe: Serialization of colleciton Pin
SeMartens11-Mar-10 1:30
SeMartens11-Mar-10 1:30 
GeneralRe: Serialization of colleciton Pin
johnjitu11-Mar-10 2:22
johnjitu11-Mar-10 2:22 
AnswerRe: Serialization of colleciton Pin
darkelv11-Mar-10 2:55
darkelv11-Mar-10 2:55 
QuestionVS 2008 and Linux ?? Pin
AmbiguousName10-Mar-10 19:25
AmbiguousName10-Mar-10 19:25 
AnswerRe: VS 2008 and Linux ?? Pin
Rod Kemp10-Mar-10 19:42
Rod Kemp10-Mar-10 19:42 
AnswerRe: VS 2008 and Linux ?? Pin
Eddy Vluggen11-Mar-10 2:18
professionalEddy Vluggen11-Mar-10 2:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.