473,473 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Get Calling Function Info

Hi,

I built many classes to be used. One function can call one or more other
functions. If any of these functions have an error I add the error
description into a log file. My problem is to identify all calling functions,
ie, I want to get the full path of called function: f1 calls f2, and f2 calls
f3, then f3 gets an error, I want to get f1, f2 names. Is that a way to do it?
Thanks in advance
Nov 2 '06 #1
5 1309
See the online help in Visual Studio for the System.Diagnostics.StackTrace
class. There is a warning with that class that it doesn't provide as detailed
information when running a program compiled as "Release."

If you are using Visual Basic 2005, you can also access the following property,
which returns a string with the entire stack trace.

My.Application.Info.StackTrace

-----
Tim Patrick
Start-to-Finish Visual Basic 2005
Hi,

I built many classes to be used. One function can call one or more
other functions. If any of these functions have an error I add the
error description into a log file. My problem is to identify all
calling functions, ie, I want to get the full path of called function:
f1 calls f2, and f2 calls f3, then f3 gets an error, I want to get f1,
f2 names. Is that a way to do it? Thanks in advance

Nov 2 '06 #2
Li Pang wrote:
I want to get the full path of called function: f1 calls f2, and f2 calls
f3, then f3 gets an error
Is that a way to do it?
It's all wrapped up in the Exception class.

Try
CallFunctionThatFails()

Catch ex As Exception

LogToFile( ex.ToString() )
' or extract the .Message and .StackTrace properties

End Try

It doesn't matter how many levels down into your code you go; the
Exception will contain the complete Stack, all the way down. Usually.

HTH,
Phill W.
Nov 2 '06 #3
So is it practice to do a Try Catch - prior to every function / procedure
call ?

M.

"Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-kwrote in message
news:ei**********@south.jnrs.ja.net...
Li Pang wrote:
>I want to get the full path of called function: f1 calls f2, and f2 calls
f3, then f3 gets an error Is that a way to do it?

It's all wrapped up in the Exception class.

Try
CallFunctionThatFails()

Catch ex As Exception

LogToFile( ex.ToString() )
' or extract the .Message and .StackTrace properties

End Try

It doesn't matter how many levels down into your code you go; the
Exception will contain the complete Stack, all the way down. Usually.

HTH,
Phill W.

Nov 2 '06 #4
Hi,

As long as I think, it is possible by writing Try-Catch in all procedures
and using Throw.
But, this is troublesome?(^^)

sample

Public Class Form1

Dim r As New System.Random()

Private Sub testA()
Try
Dim i As Integer

i = r.Next(3)

Dim j As Integer
j = 5 / i
testA()
Catch ex As Exception
Dim st As New System.Diagnostics.StackTrace(ex)

Throw New Exception(ex.Message & vbCrLf & st.ToString, ex)
End Try
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
testA()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Debug.WriteLine("END")

End Sub
End Class

Nov 2 '06 #5
Miro wrote:
So is it practice to do a Try Catch - prior to every function / procedure
call ?
No. Definitely not.

You /only/ add Catch blocks when you can do something *useful* with the
Exception if happens, as in

Sub ReloadConfiguration()
Try

' [re-]read options from file
Dim sr As New StreamReader( "file" )
. . .
sr.Close()

Catch ex As FileNotFoundException

' [Re-]Apply /default/ options here

End Try
End Sub

Catching and rethrowing an Exception in every code routine is pointless,
redundant and just slows the code down.

Logging the exception /only/ counts as "useful" if your code is
providing a [central] service for a [remote] client. The logged version
is useful to you in diagnosing the problem, but probably not to your
clients. They /might/ want to get an Exception back (re-Throw it), or
you may ahve to "translate" it into something useful to them (e.g. an
error flag in the result returned by a WebService).

HTH,
Phill W.
Nov 6 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ken | last post by:
hello, I would to know if it is possible to call an object in a function within a class. Meaning , In a class, A function X calling onto a function Y, and function Y we want one of the two...
3
by: Glenn C. Rhoads | last post by:
I'm having a problem calling a C function from a publicly available library file. To get the code to compile, I had to declare the function as an external C function as follows. extern "C"...
5
by: benc | last post by:
Hi Can some good soul help on this. I need to call jscript functions from C#. I have hosted a web control and displayed an html page successfully, but just can't find a way to call jscript...
5
by: Al the programmer | last post by:
I have a dll that consists of unmanaged C++ routines. I can call a function in the dll from a C++ executable, but not from a C# executable. I am calling an independent function, not a class. C#...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
3
by: Benjamin Bittner | last post by:
Hallo NG, I have problems with calling the CreateProcessWithLogonW() function. I tried converting an VB6 example (http://support.microsoft.com/default.aspx?scid=kb;en-us;285879) and some snippets...
9
by: Laurent | last post by:
Hello, I received a C++ DLL and I must include it in my C# programm. The problem is that I don't know how to convert the parameters from C++ to C#, specialy when there are pointers in it....
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
15
by: dspfun | last post by:
Hi, Is it possible to print the function name of the calling function? For example, f1() and f2() both calls f3(), in f3() I would like to print the name of the function calling f3() which...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.