473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

try\catch getting the right line number

I have a block of code that during development is prone to casting errors.
It is mostly a DataReader type thing.

It looks something like this.

_prtPNID = myDLReader.GetString("prtPNID")
_prtSKU = myDLReader.GetString("prtSKU")
_prtPic = myDLReader.GetString("prtPic")
_prtRsvQty = myDLReader.GetInteger("prtRsvQty")

myDLReader.close

If I don't put it in a try catch block, I can get the right line number as
it bubbles out to try\catch outward. But because I need to put his same
thing into a Transaction, I get an error if I don't get to the
MyDlReader.Close() before the rollback AND the rollback comes before the
Handling of the exception (logging, gui msg, etc). So the original error
gets swallowed up.

So easy enough, put it all into a Try\Catch block and Ensure the reader gets
closed. But as the original exception is rethrown (Throw or Throw ex) the
line number changes to the subsequent Throw line and the "real" line number
is lost.

Try
_prtPNID = myDLReader.GetString("prtPNID")
_prtSKU = myDLReader.GetString("prtSKU")
_prtPic = myDLReader.GetString("prtPic")
_prtRsvQty = myDLReader.GetInteger("prtRsvQty")
catch (ex as exception)
MyDlReader.Close() <--- at this point the ex.StackTrace has the
line number i want
Throw ex <--- the catcher of this gets StackTrace
as being line (aargh)
end try

Ideas?

Thanks,
jeff
Apr 6 '07 #1
4 2988
On Apr 5, 8:20 pm, "Jeff Jarrell" <jjarrel_NOS...@yahoo.comwrote:
I have a block of code that during development is prone to casting errors.
It is mostly a DataReader type thing.

It looks something like this.

_prtPNID = myDLReader.GetString("prtPNID")
_prtSKU = myDLReader.GetString("prtSKU")
_prtPic = myDLReader.GetString("prtPic")
_prtRsvQty = myDLReader.GetInteger("prtRsvQty")

myDLReader.close

If I don't put it in a try catch block, I can get the right line number as
it bubbles out to try\catch outward. But because I need to put his same
thing into a Transaction, I get an error if I don't get to the
MyDlReader.Close() before the rollback AND the rollback comes before the
Handling of the exception (logging, gui msg, etc). So the original error
gets swallowed up.

So easy enough, put it all into a Try\Catch block and Ensure the reader gets
closed. But as the original exception is rethrown (Throw or Throw ex) the
line number changes to the subsequent Throw line and the "real" line number
is lost.

Try
_prtPNID = myDLReader.GetString("prtPNID")
_prtSKU = myDLReader.GetString("prtSKU")
_prtPic = myDLReader.GetString("prtPic")
_prtRsvQty = myDLReader.GetInteger("prtRsvQty")
catch (ex as exception)
MyDlReader.Close() <--- at this point the ex.StackTrace has the
line number i want
Throw ex <--- the catcher of this gets StackTrace
as being line (aargh)
end try

Ideas?

Thanks,
jeff
Why do you need to know the line number in the first place? In the
release version line numbers will not exist, so you can't use it for
error logging.

Also, if you want to make sure the datareader gets closed you should
put MyDlReader.Close() in a finally block of the try...end try
statement.

Thanks,

Seth Rowe

Apr 6 '07 #2
Jeff Jarrell wrote:
I have a block of code that during development is prone to casting errors.
It is mostly a DataReader type thing.

It looks something like this.

_prtPNID = myDLReader.GetString("prtPNID")
_prtSKU = myDLReader.GetString("prtSKU")
_prtPic = myDLReader.GetString("prtPic")
_prtRsvQty = myDLReader.GetInteger("prtRsvQty")

myDLReader.close

If I don't put it in a try catch block, I can get the right line number as
it bubbles out to try\catch outward. But because I need to put his same
thing into a Transaction, I get an error if I don't get to the
MyDlReader.Close() before the rollback AND the rollback comes before the
Handling of the exception (logging, gui msg, etc). So the original error
gets swallowed up.

So easy enough, put it all into a Try\Catch block and Ensure the reader gets
closed. But as the original exception is rethrown (Throw or Throw ex) the
line number changes to the subsequent Throw line and the "real" line number
is lost.

Try
_prtPNID = myDLReader.GetString("prtPNID")
_prtSKU = myDLReader.GetString("prtSKU")
_prtPic = myDLReader.GetString("prtPic")
_prtRsvQty = myDLReader.GetInteger("prtRsvQty")
catch (ex as exception)
MyDlReader.Close() <--- at this point the ex.StackTrace has the
line number i want
Throw ex <--- the catcher of this gets StackTrace
Just use:

Throw
as being line (aargh)
end try

Ideas?

Thanks,
jeff
--
Göran Andersson
_____
http://www.guffa.com
Apr 6 '07 #3
The line numbers are ONLY needed during development.

Currently I am involved in a large port\migration effort. Aside from a
signficant Vb6\Php migration to .Net we are also moving from where we only
supported MySQL into an database neutral environment where we will be
supporting multiple databases (mysql, mssql, and possibly oracle. Needless
to say the the underlying data types are different. The way we are managing
this is to map out the data type differences within StoredProcedures. Each
DB has its own set of stored procedures. Now on the .Net side we have these
Codesmith generated wrappers. One wrapper for all of the DB Types. The
frameworks maps it out to the proper database and underlying typed data
objects (connections, parms, etc).

Since we have one wrapper per logical SP, i expect this to be a frequent
source of error in development primarily because the SPs themselves are for
the most part hand coded.

Thanks,
jeff

"rowe_newsgroups" <ro********@yahoo.comwrote in message
news:11**********************@o5g2000hsb.googlegro ups.com...
On Apr 5, 8:20 pm, "Jeff Jarrell" <jjarrel_NOS...@yahoo.comwrote:
>I have a block of code that during development is prone to casting
errors.
It is mostly a DataReader type thing.

It looks something like this.

_prtPNID = myDLReader.GetString("prtPNID")
_prtSKU = myDLReader.GetString("prtSKU")
_prtPic = myDLReader.GetString("prtPic")
_prtRsvQty = myDLReader.GetInteger("prtRsvQty")

myDLReader.close

If I don't put it in a try catch block, I can get the right line number
as
it bubbles out to try\catch outward. But because I need to put his same
thing into a Transaction, I get an error if I don't get to the
MyDlReader.Close() before the rollback AND the rollback comes before the
Handling of the exception (logging, gui msg, etc). So the original error
gets swallowed up.

So easy enough, put it all into a Try\Catch block and Ensure the reader
gets
closed. But as the original exception is rethrown (Throw or Throw ex)
the
line number changes to the subsequent Throw line and the "real" line
number
is lost.

Try
_prtPNID = myDLReader.GetString("prtPNID")
_prtSKU = myDLReader.GetString("prtSKU")
_prtPic = myDLReader.GetString("prtPic")
_prtRsvQty = myDLReader.GetInteger("prtRsvQty")
catch (ex as exception)
MyDlReader.Close() <--- at this point the ex.StackTrace has the
line number i want
Throw ex <--- the catcher of this gets
StackTrace
as being line (aargh)
end try

Ideas?

Thanks,
jeff

Why do you need to know the line number in the first place? In the
release version line numbers will not exist, so you can't use it for
error logging.

Also, if you want to make sure the datareader gets closed you should
put MyDlReader.Close() in a finally block of the try...end try
statement.

Thanks,

Seth Rowe

Apr 6 '07 #4
Goran,

Believe it or not I had tried it both ways. It didn't work.

I guess my out is to just grab the stack trace at that point and parse it. I
know I want it if the particular line contains "LoadRow"). I started to go
down working through the stack frames (unsuccessfully so far) before the
throw, but you know a simple regex on text will take care of it.

Thanks,
jeff

===== this is the stack trace before I throw ============
ex.StackTrace " at System.Data.SqlClient.SqlBuffer.get_Int16()
at System.Data.SqlClient.SqlDataReader.GetInt16(Int32 i)
at whi.pw.core.dal.DLReader.GetShortInteger(String fldName) in
N:\core\core\datalayer\DLReader.cs:line 189
at whi.pw.core.vb.tests.yyyUpdateDemo.RowValues.LoadR ow(DLReader
myDLReader) in N:\core\core.vb.tests\sp_wrappers\yyyUpdateDemo.vb :line 354"
String

======== The part that I am looking for is the "line 354" thing above
==============

==== Here is the stack trace in the caller, after I THROW per your
suggestion =====

StackTrace " at System.Data.SqlClient.SqlBuffer.get_Int16()\r\n
at System.Data.SqlClient.SqlDataReader.GetInt16(Int32 i)\r\n
at whi.pw.core.dal.DLReader.GetShortInteger(String fldName)
in N:\\core\\core\\datalayer\\DLReader.cs:line 189\r\n
at whi.pw.core.vb.tests.yyyUpdateDemo.RowValues.LoadR ow(DLReader
myDLReader)
in N:\\core\\core.vb.tests\\sp_wrappers\\yyyUpdateDem o.vb:line
549\r\n
at whi.pw.core.vb.tests.yyyUpdateDemo.Read()
in N:\\core\\core.vb.tests\\sp_wrappers\\yyyUpdateDem o.vb:line 62\r\n
at whi.pw.core.vb.tests.yyyUpdateDemoTask.Execute(Tas kToken myTaskToken)
in N:\\core\\core.vb.tests\\PipelineTasks\\yyyUpdateD emoTask.vb:line
20\r\n
at whi.pw.core.dal.PipelineBase.Execute(TaskToken myTaskToken) in
N:\\core\\core\\datalayer\\pipeline\\PipelineBase. cs:line 38" string

=== The "line 549" is the THROW line ====== Aargh ====

"Göran Andersson" <gu***@guffa.comwrote in message
news:eJ****************@TK2MSFTNGP02.phx.gbl...
Jeff Jarrell wrote:
>I have a block of code that during development is prone to casting
errors. It is mostly a DataReader type thing.

It looks something like this.

_prtPNID = myDLReader.GetString("prtPNID")
_prtSKU = myDLReader.GetString("prtSKU")
_prtPic = myDLReader.GetString("prtPic")
_prtRsvQty = myDLReader.GetInteger("prtRsvQty")

myDLReader.close

If I don't put it in a try catch block, I can get the right line number
as it bubbles out to try\catch outward. But because I need to put his
same thing into a Transaction, I get an error if I don't get to the
MyDlReader.Close() before the rollback AND the rollback comes before the
Handling of the exception (logging, gui msg, etc). So the original error
gets swallowed up.

So easy enough, put it all into a Try\Catch block and Ensure the reader
gets closed. But as the original exception is rethrown (Throw or Throw
ex) the line number changes to the subsequent Throw line and the "real"
line number is lost.

Try
_prtPNID = myDLReader.GetString("prtPNID")
_prtSKU = myDLReader.GetString("prtSKU")
_prtPic = myDLReader.GetString("prtPic")
_prtRsvQty = myDLReader.GetInteger("prtRsvQty")
catch (ex as exception)
MyDlReader.Close() <--- at this point the ex.StackTrace has the
line number i want
Throw ex <--- the catcher of this gets
StackTrace

Just use:

Throw
>as being line (aargh)
end try

Ideas?

Thanks,
jeff

--
Göran Andersson
_____
http://www.guffa.com

Apr 6 '07 #5

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

Similar topics

4
by: rolf | last post by:
Hmm, I seem to be missing something basic with regards to SAXParsers. I'm parsing some XML using Java 1.4.2 and SAX, so I extend DefaultHandler and in my startElement() method I detect some kind...
11
by: Ken Varn | last post by:
I want to be able to determine my current line, file, and function in my C# application. I know that C++ has the __LINE__, __FUNCTION__, and __FILE___ macros for getting this, but I cannot find a...
4
by: John Galt | last post by:
I have a stacktrace that gives the line number of the offending code, but for the life of me can't seem to find a display of the line number in the Visual Studio.Net code editor. I'm sure I've seen...
3
by: Rotsey | last post by:
Hi, I am trying to write some error handling code to log the error. But getting the line number of the error is not simple. Is it because the app is set to "Release" and does not have debug...
1
by: pukya78 | last post by:
Hi, I am trying to get the current file and the line number which is getting executed. I used: MessageBox.Show(New StackTrace(New StackFrame(True)).GetFrame(0).GetFileLineNumber) which gives me...
9
by: Adrienne Boswell | last post by:
I am getting this error: Error Type: Microsoft VBScript runtime (0x800A000D) Type mismatch: 'sid' /beta/files/index.asp There is no line number, and there is absolutely nothing on that page...
2
by: John | last post by:
Hi For error logging purpose, is it possible to get from the vb system feature the SUB name and the line number? Thanks Regards
5
by: IdleBrain | last post by:
I am trying to log the Application name, Method name, line number and column number whenever an exception is generated in an C# 2005 application using the following code. Problem is that the...
0
by: Rajgodfather | last post by:
I am getting the end couldn't error while validating xml file against xsd. The xml file looks perfect. XML: <event id="1"> <!-- Successful event. --> ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.