bigmazi
bigmazi
CC#
Created by bigmazi on 11/30/2023 in #help
Getting call stack that was a thing before exception was thrown
I think it could be fixed like this
public static Exception ReThrow(this Exception ex)
{
var exInfo = ExceptionDispatchInfo.Capture(ex);
exInfo.Throw();

Debug.Assert(false);
return null;
}

public int ExceptionTest()
{
try
{
return GetInt();
}
catch (Exception ex)
{
throw ex.ReThrow();
}
}
public static Exception ReThrow(this Exception ex)
{
var exInfo = ExceptionDispatchInfo.Capture(ex);
exInfo.Throw();

Debug.Assert(false);
return null;
}

public int ExceptionTest()
{
try
{
return GetInt();
}
catch (Exception ex)
{
throw ex.ReThrow();
}
}
37 replies
CC#
Created by bigmazi on 11/30/2023 in #help
Getting call stack that was a thing before exception was thrown
comment there
ReThrow() is a great idea, but unlike "throw e;" it now gives an error saying "not all code paths return a value" - any idea how to fix properly? – mcmillab Dec 17, 2019 at 2:13
37 replies
CC#
Created by bigmazi on 11/30/2023 in #help
Getting call stack that was a thing before exception was thrown
looks promising
37 replies
CC#
Created by bigmazi on 11/30/2023 in #help
Getting call stack that was a thing before exception was thrown
snippets from there: #1
public void PreserveStackTrace(Exception ex)
{
MethodInfo preserve = ex.GetType().GetMethod("InternalPreserveStackTrace",
BindingFlags.Instance | BindingFlags.NonPublic);
preserve.Invoke(ex,null);
}

public void ExceptionTest()
{
try
{
throw new Exception("An Error Happened");
}
catch (Exception ex)
{
PreserveStackTrace(ex);
throw ex;
}
}
public void PreserveStackTrace(Exception ex)
{
MethodInfo preserve = ex.GetType().GetMethod("InternalPreserveStackTrace",
BindingFlags.Instance | BindingFlags.NonPublic);
preserve.Invoke(ex,null);
}

public void ExceptionTest()
{
try
{
throw new Exception("An Error Happened");
}
catch (Exception ex)
{
PreserveStackTrace(ex);
throw ex;
}
}
#2
public static void ReThrow(this Exception ex)
{
var exInfo = ExceptionDispatchInfo.Capture(ex);
exInfo.Throw();
}

public void ExceptionTest()
{
try
{
throw new Exception("An Error Happened");
}
catch (Exception ex)
{
ex.ReThrow();
}
}
public static void ReThrow(this Exception ex)
{
var exInfo = ExceptionDispatchInfo.Capture(ex);
exInfo.Throw();
}

public void ExceptionTest()
{
try
{
throw new Exception("An Error Happened");
}
catch (Exception ex)
{
ex.ReThrow();
}
}
37 replies
CC#
Created by bigmazi on 11/30/2023 in #help
Getting call stack that was a thing before exception was thrown
37 replies
CC#
Created by bigmazi on 11/30/2023 in #help
Getting call stack that was a thing before exception was thrown
worked fine
37 replies
CC#
Created by bigmazi on 11/30/2023 in #help
Getting call stack that was a thing before exception was thrown
vice versa, sharing debug build without pdb not going produce lines
37 replies
CC#
Created by bigmazi on 11/30/2023 in #help
Getting call stack that was a thing before exception was thrown
in release lines may be erased, but some of them still may be present
37 replies
CC#
Created by bigmazi on 11/30/2023 in #help
Getting call stack that was a thing before exception was thrown
it's when there is a .pdb file present
37 replies
CC#
Created by bigmazi on 11/30/2023 in #help
Getting call stack that was a thing before exception was thrown
I think I need to check whether stuff like null dereference keep original line or not, hmm...
37 replies
CC#
Created by bigmazi on 11/30/2023 in #help
Getting call stack that was a thing before exception was thrown
but it only happens if I throw explicitly from inside the try block, right?
37 replies
CC#
Created by bigmazi on 11/30/2023 in #help
Getting call stack that was a thing before exception was thrown
also, another thing I don't like is that rethrowing exception changes the line of exception origin even with just bare throw;
37 replies
CC#
Created by bigmazi on 11/30/2023 in #help
Getting call stack that was a thing before exception was thrown
I have a catch inside main for unhandled exceptions that creates a report and closes app
37 replies
CC#
Created by bigmazi on 11/30/2023 in #help
Getting call stack that was a thing before exception was thrown
ty, I'll look into it
37 replies
CC#
Created by bigmazi on 11/30/2023 in #help
Getting call stack that was a thing before exception was thrown
no, it's not to decide, it's to create report so me and other devs could look at it
37 replies
CC#
Created by bigmazi on 11/30/2023 in #help
Getting call stack that was a thing before exception was thrown
this one looks promising, I'm gonna try it
37 replies
CC#
Created by bigmazi on 11/30/2023 in #help
Getting call stack that was a thing before exception was thrown
the intention is to get error reports from customers
37 replies
CC#
Created by bigmazi on 11/30/2023 in #help
Getting call stack that was a thing before exception was thrown
sometimes I call code made by other people and I know I can recover at a particular point without entering a corrupted state, I just don't know what exactly they gonna throw
37 replies
CC#
Created by bigmazi on 11/30/2023 in #help
Getting call stack that was a thing before exception was thrown
catch (Exception ex)
{
ex.StackTrace + Environment.StackTrace;
}
catch (Exception ex)
{
ex.StackTrace + Environment.StackTrace;
}
37 replies
CC#
Created by bigmazi on 11/30/2023 in #help
Getting call stack that was a thing before exception was thrown
ig I can combine stack trace that I get inside handler with the one I get from exception?
37 replies