Welcome
Ladies and Gents:

These forums are now closed and registration disabled.

Please join us at our new forum on Proboards. Our hope is that these new forums are more stable, provide more and better features, and allow continuation of the project forums in a safer, more secure, long term environment.

me3explorer.proboards.com

--The ME3Explorer Team

Advanced try/catch

Coder-centric area for programming advice and questions.

Advanced try/catch

Postby WarrantyVoider » 09 Nov 2014, 13:47

I came along a problem, were I needed to find the exact line of an error and the full call stack while NOT in the IDE, means while running. well heres my attempt at it, the idea is to wrap every function except the top calling functions (mainloop f.e.) in these try catch statements, only the top caller finally prints it out (or processes it, logs it, whatever).



how does one get the call stack? well the idea would be to catch the error, add your function name and throw it further up, like this:
Spoiler:
void Test1()
{
try
{
Test2();
}
catch (Exception Ex)
{
throw new Exception("Test 1 had an error:\n" + Ex.Message);
}
}


but it would be a waste of effort to write every function name per hand, luckily c# supports reflection so I can get this on runtime:
Spoiler:
using System.Runtime.CompilerServices;
using System.Diagnostics;

[MethodImpl(MethodImplOptions.NoInlining)]
public string MakeErrorMsg(Exception Ex)
{
StackTrace st = new StackTrace();
StackFrame sf = st.GetFrame(1);
return this.GetType().FullName + "." + sf.GetMethod().Name + "()" + " had an error: \n" + Ex.Message;
}

void Test1()
{
try
{
Test2();
}
catch (Exception Ex)
{
throw new Exception(MakeErrorMsg(Ex));
}
}


finally I needed the linenumber, but that wasnt so hard to add:
Spoiler:
using System.Runtime.CompilerServices;
using System.Diagnostics;

[MethodImpl(MethodImplOptions.NoInlining)]
public string MakeErrorMsg(Exception Ex)
{
StackTrace st = new StackTrace();
StackFrame sf = st.GetFrame(1);
int lineNumber = new System.Diagnostics.StackTrace(Ex, true).GetFrame(0).GetFileLineNumber();
return this.GetType().FullName + "." + sf.GetMethod().Name + "()" + " had an error, line: " + lineNumber + "\n" + Ex.Message;
}

void Test1()
{
try
{
Test2();
}
catch (Exception Ex)
{
throw new Exception(MakeErrorMsg(Ex));
}
}


so there it is^^ one could probably add the function arguments and types and values aswell, but I didnt needed it for this, so I leave that out for now^^

@KFreon: I think this could help you better track down bugs and errors, but I dont know how much effort it would be for you to implement it, so this is just a suggestion :)

greetz WV
always backup your files!
mess with the best or die like the rest!
"I tried everything!" - "mkay, please list that..." ; please dont pm me for help, we have a help section
User avatar
WarrantyVoider
Emeritus
 
Posts: 2270
Joined: 22 Aug 2012, 11:33
Has thanked: 480 time
Have thanks: 626 time

Re: Advanced try/catch

Postby WarrantyVoider » 09 Nov 2014, 13:57

I should have googled more before I wrote this, theres an even better way: getting callstack at ANY point:

http://www.codeproject.com/Articles/223611/How-to-log-the-current-call-stack-in-NET

greetz WV
always backup your files!
mess with the best or die like the rest!
"I tried everything!" - "mkay, please list that..." ; please dont pm me for help, we have a help section
User avatar
WarrantyVoider
Emeritus
 
Posts: 2270
Joined: 22 Aug 2012, 11:33
Has thanked: 480 time
Have thanks: 626 time


Return to Coders' Help

Who is online

Users browsing this forum: No registered users and 1 guest

suspicion-preferred