locked
Exceptions catch? RRS feed

  • Question

  • Hello...

    How can i get the name (or whatever indication) of method that creates an exception at the method which it use? i.e:

    MathodA()

    {

    try { /*DoSomething*/ }

    catch(Exception Ex) { MessageBox.Show("Ex="+Ex.ToString()); }

    }

    MethodB() { MathodA() }

    MethodC() { MathodA() }

    MethodD() { MathodA() }

    MethodE() { MathodA() }

    Now i get the exception message from MethodA (There is also TryCatch blox at Mathods B C D and E, if it's matter), There is no more then one method uses MethodA simultaneously.

     

    I try many properties of Exception (I do not know why but TatgetSite of TargetSite not works). 

     

    How can i manage that issue?

     

    Thanks

     

     

     

    Friday, September 2, 2011 10:33 PM

Answers

  • You will probably need to use a custom event and a custom class instance that derives from EventArgs, with property that contains the method name.  The following code returns the name of the current method, the method that contains the code.

            static string GetCurrentMethodName()
            {
                string methodName = String.Empty;
                MethodBase mb = MethodInfo.GetCurrentMethod();
                methodName = mb.Name;
                return methodName;
            }
    

    I cannot tell you anymore than that.  Not enough info from you.  How you go about it depends upon your code and what it does.

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    • Marked as answer by YakirCohen Saturday, September 3, 2011 3:37 AM
    Friday, September 2, 2011 11:00 PM
  • Hay nice stamp "fooling computers..." i remember i saw it somewhere and liked it...

     

    Okay i see, is there any information i can give that can describe the issue?

     

    Thanks.

    • Marked as answer by YakirCohen Saturday, September 3, 2011 3:37 AM
    Saturday, September 3, 2011 2:27 AM
  • You should be able to get the name of the method that threw it from the stack trace property on the exception http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx.

    Or you could create your own exception type, that would have a throwingmethod field, and when constructing the exception pass it the result of MethodBase.GetCurrentMethod http://msdn.microsoft.com/en-us/library/system.reflection.methodbase.getcurrentmethod.aspx to put into the field.

    • Marked as answer by YakirCohen Saturday, September 3, 2011 3:37 AM
    Saturday, September 3, 2011 3:01 AM
  • TargetSite property should work (I verified through below code ). In below code, it shows methodName as "MethodA" since it threw the exception.

    public void MethodA()
    {
             throw new Exception();
    }
    
    public void MethodB()
    {
             MethodA();
    }
    
    public void MethodC()
    {
             MethodB();
    }
    
    public void SomeMethod()
    {
            try
            {
            }
            catch(Exception excep)
            {
                    string methodName = e.TargetSite.Name;
            }
    }
    

     


    Please mark this post as answer if it solved your problem. Happy Programming!
    • Marked as answer by YakirCohen Saturday, September 3, 2011 3:37 AM
    Saturday, September 3, 2011 3:11 AM

All replies

  • You will probably need to use a custom event and a custom class instance that derives from EventArgs, with property that contains the method name.  The following code returns the name of the current method, the method that contains the code.

            static string GetCurrentMethodName()
            {
                string methodName = String.Empty;
                MethodBase mb = MethodInfo.GetCurrentMethod();
                methodName = mb.Name;
                return methodName;
            }
    

    I cannot tell you anymore than that.  Not enough info from you.  How you go about it depends upon your code and what it does.

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    • Marked as answer by YakirCohen Saturday, September 3, 2011 3:37 AM
    Friday, September 2, 2011 11:00 PM
  • Hay nice stamp "fooling computers..." i remember i saw it somewhere and liked it...

     

    Okay i see, is there any information i can give that can describe the issue?

     

    Thanks.

    • Marked as answer by YakirCohen Saturday, September 3, 2011 3:37 AM
    Saturday, September 3, 2011 2:27 AM
  • You should be able to get the name of the method that threw it from the stack trace property on the exception http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx.

    Or you could create your own exception type, that would have a throwingmethod field, and when constructing the exception pass it the result of MethodBase.GetCurrentMethod http://msdn.microsoft.com/en-us/library/system.reflection.methodbase.getcurrentmethod.aspx to put into the field.

    • Marked as answer by YakirCohen Saturday, September 3, 2011 3:37 AM
    Saturday, September 3, 2011 3:01 AM
  • TargetSite property should work (I verified through below code ). In below code, it shows methodName as "MethodA" since it threw the exception.

    public void MethodA()
    {
             throw new Exception();
    }
    
    public void MethodB()
    {
             MethodA();
    }
    
    public void MethodC()
    {
             MethodB();
    }
    
    public void SomeMethod()
    {
            try
            {
            }
            catch(Exception excep)
            {
                    string methodName = e.TargetSite.Name;
            }
    }
    

     


    Please mark this post as answer if it solved your problem. Happy Programming!
    • Marked as answer by YakirCohen Saturday, September 3, 2011 3:37 AM
    Saturday, September 3, 2011 3:11 AM
  • Hay...

    When i use TargetSite.Name on my code i get 'SomeMethod' because that is where actually the exception occurred right?, but what i want to get is MathodA that triggered the exception while trying to use SomeMethod, let's say i have 100 methods uses SomeMethod and i get exception one time so obviously there's no problem with SomeMethod and there is problem with one other methods uses SomeMethod. i want to get the specific method that has the problem... the method that triggers the exception in the method it uses...      

    Sunday, September 4, 2011 2:24 AM
  • It will give you methodA only. (I have made a mistake in my above code that I forgot to write MethodA inside Try). Let me correct that.

            public void MethodA()
            {
                throw new Exception();
            }
    
            public void MethodB()
            {
                MethodA();
            }
    
            public void MethodC()
            {
                MethodB();
            }
    
            public void SomeMethod()
            {
                try
                {
                    MethodA();
                }
                catch (Exception excep)
                {
                     MessageBox.Show(excep.TargetSite.Name);    //This will print "MethodA"
                }
            }
    


    Try this code.


    Please mark this post as answer if it solved your problem. Happy Programming!
    Sunday, September 4, 2011 3:51 AM
  •  

    That’s not what I meant to… let’s say

     

    //--------------------------------

     

    public int MainMathod ( int MyInt )

    {

                try {return MyInt *3;}

                catch(Exception MyEx) { MessageBox.Show(“Exception caught: ”+MyEx.TargetSite.Name); return 0;}

    }

     

    private void btn1_Click(object sender, EventArgs e)

    {

                Console.WriteLine(MainMathod(2));

    }

    private void btn2_Click(object sender, EventArgs e)

    {

                Console.WriteLine(MainMathod(-4));

    }

    private void btn3_Click(object sender, EventArgs e)

    {

                Console.WriteLine(MainMathod(1.5));

    }

     

    //----------------------------------------

     

    Now when I’ll click btn1 and btn2 the Console will show 6 and -12, but if I’ll click btn3 I should get an Exception for sending Double value instead of Integer value, but MyEx.TargetSite.Name will give me “MainMethod” when what I am looking for is “btn3” that uses MainMethod in a wrong way…

     

    • Edited by YakirCohen Wednesday, September 7, 2011 10:01 AM
    Wednesday, September 7, 2011 10:00 AM
  • If you want to know which methods are in the call stack, you can use the StackTrace class.

    To know which method called the method which thrown the exception:

    new StackTrace(MyEx).GetFrame(1).GetMethod().Name

    To know which method called the currently running method:

    new StackTrace().GetFrame(1).GetMethod().Name

    Wednesday, September 7, 2011 4:51 PM
  • I would hesitate to use StackTrace in Release builds. 

    http://msdn.microsoft.com/en-us/library/system.diagnostics.stacktrace(v=VS.100).aspx

    It is good for diagnostics in Debug builds, but can give you unexpected or unreliable results with release builds.  I understand your approach and problem, but I ask what issue are you trying to resolve.  Perhaps, there is an alternative solution.

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    Wednesday, September 7, 2011 9:42 PM
  •  

    Now when I’ll click btn1 and btn2 the Console will show 6 and -12, but if I’ll click btn3 I should get an Exception for sending Double value instead of Integer value, but MyEx.TargetSite.Name will give me “MainMethod” when ...

    ...what I am looking for is “btn3” that uses MainMethod in a wrong way

    Are you looking to resolve a compile time issue?


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    Wednesday, September 7, 2011 9:47 PM
  • Are you looking to resolve a compile time issue?

    I had overlooked that. The code cannot throw any exception, since it cannot be compiled.
    Thursday, September 8, 2011 2:13 PM
  • I know what you're trying to accomplish, but why are you trying to accomplish it? If the purpose is to print it out to a diagnostic log, or to a message box on the screen, so that a human can look at the problem, exception.ToString() is what you want.

    If you're trying to have the code use this information to make decisions (and no human is involved) I think that even if you figure out the code you want, you're in for a world of hurt. I've never known any program that does that, and it sounds like to wrong solution to a problem to me.

    Saturday, September 10, 2011 4:22 PM
  • If you want to know which methods are in the call stack, you can use the StackTrace class.

    To know which method called the method which thrown the exception:

    new StackTrace(MyEx).GetFrame(1).GetMethod().Name

    To know which method called the currently running method:

    new StackTrace().GetFrame(1).GetMethod().Name

     

    I did it like that (within the catch block):

     

     

    StackTrace ST = new StackTrace(MyEx);

    strST = ST.GetFrame(1).GetMethod().Name;

    Console.WriteLine(strST);

     

    And it's doesn't show anything. and it gives me an exception on that - Object reference not set to an instance of an object.

     


     

     

    Thursday, September 15, 2011 12:34 PM
  • You should check to see what FrameCount is and not request a frame greater than than.
    Thursday, September 15, 2011 2:07 PM
  • You should check to see what FrameCount is and not request a frame greater than than.
    I do not know how to check that, but i try Zero and it could't be less than that right? it still doesn't show anything...
    Friday, September 16, 2011 2:57 PM
  • FrameCount is a field on the StackTrace class.
    Friday, September 16, 2011 3:45 PM
  • I've managed to extract the method names that cause the exceptions thanks...

    However it's still complicated than i though and the functions are large and seems to alright, i need to pin point the line and column in the code that has the problem. is it possible?

     

    Thanks all.


    • Edited by YakirCohen Saturday, September 17, 2011 7:11 PM
    Saturday, September 17, 2011 7:11 PM
  • I'm going to go back to one of my original questions and ask why you need it? What are you trying to do with this information?
    Saturday, September 17, 2011 8:35 PM
  • I'm going to go back to one of my original questions and ask why you need it? What are you trying to do with this information?


    Good idea.  I still have no idea what you have been talking about.  Your code snippet doesn't even compile.  Stack trace what?  I ask again.  Are you having a compile time issue?  Are you writing an application to compile code?

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    Saturday, September 17, 2011 9:35 PM