- Home /
throw exception override stacktrace double click log
With a custom exception, I'm trying to give it a stacktrace without throwing so I can throw it later (holding the same stack trace). The problem I'm having is the new StackTrace is providing a different output than throw Exception.StackTrace, most notably missing [0x00002]
which I'm guessing is what Unity is using to jump to the line number in visual studio when double-clicking the log. Is there any way to do this and have the double-click functionality still work?
public class MyException : Exception
{
public MyException(string stackTrace)
{
_stackTrace = stackTrace;
}
string _stackTrace;
public override string StackTrace
{
get
{
return _stackTrace;
}
}
}
try
{
throw new Exception(); // double-click goes right here
}
catch (Exception e)
{
Debug.Log("StackTrace: {" + e.StackTrace + "}"); // output: "StackTrace: { at TestScript.Awake () [0x00002] in /Users/lol_tim/New Unity Project/Assets/TestScript.cs:28 }"
throw new MyException(e.StackTrace);
}
try
{
throw new MyException(new System.Diagnostics.StackTrace(0, true).ToString()); // double-click does nothing
}
catch (Exception e)
{
Debug.Log("StackTrace: {" + e.StackTrace + "}"); // output: "StackTrace: { at TestScript.Awake() in /Users/lol_tim/New Unity Project/Assets/TestScript.cs:line 28}"
throw new MyException(e.StackTrace);
}
the is totally outside my knowledge, but I found this post where there's a little more going on concerning System.Diagnostics.Stacktrace. maybe it helps you:
https://answers.unity.com/questions/289006/catching-double-clicking-console-messages.html
Thanks, I read that before posting this question. I'm trying to work with the default console (any uncaught exception will get printed to it) which it looks like the top person couldn't figure out, either. I saw the last person compiles their $$anonymous$$yDebug
to a .dll which made double-click go where they want, but unfortunately I can't do that from just throwing an exception.
Answer by ProtoTerminator · Dec 13, 2018 at 06:23 PM
It looks like I can get the double-click to work by substituting a gibberish code (not really sure what that's used for) and realigning the formatting to match, at least in english culture. I'm not sure if this works with non-english cultures (I tried changing my culture before running this code, but everything still came out in english for some reason). Of course this is extremely ugly and I wish there was a better way to do it.
System.Text.StringBuilder sb = new System.Text.StringBuilder(new System.Diagnostics.StackTrace(0, true).ToString());
sb.Remove(0, 1);
sb.Append(" ");
sb.Replace("(", " (");
sb.Replace(") in", ") [0x00000] in");
sb.Replace("\n ", " \n");
sb.Replace("line ", string.Empty);
newTrace = sb.ToString();
Answer by AnKOu · Mar 28 at 02:16 PM
Convert an Exception stacktrace so that it keeps its links
private const string format = "{0} in <a href=\"{1}\" line=\"{2}\">{3}:{4}</a>";
private static string GetUnityFormatStacktrace(Exception pException )
{
string[] lines = pException.StackTrace.Split("\n");
System.Text.StringBuilder builder = new System.Text.StringBuilder();
for (int l = 0; l < lines.Length; l++)
{
string currentLine = lines[l];
if (currentLine.Contains("Assets\\") == false)
{
builder.AppendLine(currentLine);
continue;
}
string[] pathLine = currentLine.Substring(lines[l].IndexOf("Assets\\")).Split(":");
string path = pathLine[0];
string line = pathLine[1];
string newLine = string.Format(format, currentLine.Split(" in ")[0], path, line, path, line);
builder.AppendLine(newLine);
}
return builder.ToString();
}