PDA

Click to See Complete Forum and Search --> : Object to System.Exception [RESOLVED]


Kasracer
Jan 6th, 2004, 01:57 PM
I need a dynamic array, so I am using an ArrayList, however, I need it to contain Exceptions. I can add Exceptions to it fine, however, using them and trying to return just 1 of them (i.e. return ArrayList1[0]) is a problem.

When I add an Exception to the ArrayList, it converts it to an Object and once it puts it inside of the list, I cannot use it as an Exceptions anymore.

Is there a way to hold Exceptions in a dynamic array? Regular arrays with a set max just don't seem to work for some reason.

Also, is there a way to capture ALL Exceptions that happen within a program? Like some sort of an event? Or am I going to have to encapsulate everything in try/catch statements?

Incase you haven't noticed, I'm trying to write a self-contained DLL file in which writes the user's computer information to a log file, then on close, ALL exception information that happens with my program is also written to the log file. Later I'm gonna make a dialogbox out of just code to ask the user if they want to send it directly to me. This would make it alot easier to track errors on computers once it leaves the development environment.

hellswraith
Jan 6th, 2004, 09:14 PM
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/emab-rm.asp

Kasracer
Jan 6th, 2004, 09:37 PM
Originally posted by hellswraith
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/emab-rm.asp The class I already got made-up somewhat does just what I want on exit, and I know what I want it to do (ApplicationBlocks would require alot more work and this can be simple plug and play), but thanks anyway.

kovan
Jan 6th, 2004, 09:50 PM
why invent something for something that it wasnt invented for

use ApplicationBlock for this and you will have less headaches down the road.


Brian, very good find.

Kasracer
Jan 6th, 2004, 10:08 PM
Originally posted by kovan
why invent something for something that it wasnt invented for
Uh?
Originally posted by kovan
use ApplicationBlock for this and you will have less headaches down the road.
But the code I already have will fit nicely in my program's current code already.

I think I'm done with this thread though, I found a better way to do some of my logging than with an array.

pvb
Jan 7th, 2004, 12:24 AM
Well, for what it's worth, when you save any object to an arraylist, true, it's saved as an object but it still knows what type it was previous to being added to the list. So just cast it back to the original type.
using System;
using System.Collections;

namespace ExceptionArrayList
{
public class MainApp
{
public static void Main(string[] args)
{
ArrayList exceptionList = new ArrayList();
try
{
throw new Exception("Whatever");
}
catch(Exception ex)
{
exceptionList.Add(ex);
}
Exception myException = (Exception)exceptionList[0];
Console.WriteLine(myException.Message);
Console.ReadLine();
}
}
}

Kasracer
Jan 7th, 2004, 01:34 AM
Thanks, that might come in handy :D