473,396 Members | 1,913 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,396 software developers and data experts.

using try/catch as control mechansim, is it a good thing ?


I have posted a few messages in the last few days about a project I am
working on which is a quite simple parser. I ended up using the try/
catch structure as a general mechanism to control what's happening in
the program when it finds a syntax error in the file it parses. I
would like to know if this is 'an acceptable thing to do' from a
programmation point of view, or if I should keep using it only for
catch exception created withing the program.

For example i do things like that for now

// if syntax error in the parsed file
void ParseFile()
{
...
throw( "syntax error in the file at line 23, in test.dat" )
...
}

try
{
ParseFile()
}
catch( const char *msg )
{
std::cerr << msg << std::endl;
}

Thank you -mark

Mar 4 '07 #1
6 1851
ma*****@yahoo.com wrote:
I have posted a few messages in the last few days about a project I am
working on which is a quite simple parser. I ended up using the try/
catch structure as a general mechanism to control what's happening in
the program when it finds a syntax error in the file it parses. I
would like to know if this is 'an acceptable thing to do' from a
programmation point of view, or if I should keep using it only for
catch exception created withing the program.

For example i do things like that for now

// if syntax error in the parsed file
void ParseFile()
{
...
throw( "syntax error in the file at line 23, in test.dat" )
...
}

try
{
ParseFile()
}
catch( const char *msg )
{
std::cerr << msg << std::endl;
}
The subject line was a bit misleading, you aren't using exceptions as a
control mechanism, which is a bad thing, you are using then to catch an
exceptional condition, which is a good thing.

--
Ian Collins.
Mar 4 '07 #2
>
The subject line was a bit misleading, you aren't using exceptions as a
control mechanism, which is a bad thing, you are using then to catch an
exceptional condition, which is a good thing.
Thank you Ian.
So even though the exceptional condition I catch is a syntax error in
the file which is getting parsed, this approach is still okay, is it ?
It is convenient for me because of course whenever an exception is
caught the memory is released poperly. So that's why i ended up using
it in that case.

Mar 4 '07 #3
ma*****@yahoo.com wrote:
>>The subject line was a bit misleading, you aren't using exceptions as a
control mechanism, which is a bad thing, you are using then to catch an
exceptional condition, which is a good thing.


Thank you Ian.
So even though the exceptional condition I catch is a syntax error in
the file which is getting parsed, this approach is still okay, is it ?
It is convenient for me because of course whenever an exception is
caught the memory is released poperly. So that's why i ended up using
it in that case.
Well that's the way I do it in my stock XML parser. A syntax error
isn't a normal occurrence, it is an exception that interrupts the normal
flow of parsing the document. The alternative is probably a lot of
conditional code to test for the error. I'd much rather assume the
document parses and throw if an error is encountered.

--
Ian Collins.
Mar 5 '07 #4
On 3/4/07 3:44 PM, in article
11*********************@t69g2000cwt.googlegroups.c om, "ma*****@yahoo.com"
<ma*****@yahoo.comwrote:
>>
The subject line was a bit misleading, you aren't using exceptions as a
control mechanism, which is a bad thing, you are using then to catch an
exceptional condition, which is a good thing.

Thank you Ian.
So even though the exceptional condition I catch is a syntax error in
the file which is getting parsed, this approach is still okay, is it ?
It is convenient for me because of course whenever an exception is
caught the memory is released poperly. So that's why i ended up using
it in that case.
Almost by definition, an "exceptional" condition is one that a program
cannot handle in the context of its current execution state. A parser that
encounters a syntax error while parsing is in exactly that situation. By
definition, input that a parser does not recognize is input that cannot be
handled in the execution state of the parser (otherwise, if the parser were
able to recognize the input then there would be no syntax error in the first
place - although a semantic error may be present).

Therefore, it is perfectly appropriate for a parser implemented in C++ to
throw an exception whenever it encounters any input that it fails to
recognize as grammatically correct.

Greg

Mar 5 '07 #5
On Mar 5, 6:44 am, "mast...@yahoo.com" <mast...@yahoo.comwrote:
The subject line was a bit misleading, you aren't using exceptions as a
control mechanism, which is a bad thing, you are using then to catch an
exceptional condition, which is a good thing.

Thank you Ian.
So even though the exceptional condition I catch is a syntax error in
the file which is getting parsed, this approach is still okay, is it ?
It is convenient for me because of course whenever an exception is
caught the memory is released poperly. So that's why i ended up using
it in that case.
I think it also depends no what you intend to do about it. If the
parser cannot continue or do anything useful then an exception is
probably fine. If it is going to try to complete the parse anyway (and
maybe try to flag other errors) then some other error handling
mechanism is probably more suitable.

Depending on what I'm parsing I may use either mechanism. If the text
to be parsed is generated by a user it is often better to try to
identify many errors at once (like your C++ compiler no doubt does).
If it is expected that the text is generated by a program then it is
entirely appropriate to stop on the first error (as XML parsers must).

Even if it would be convenient for your users to get more than one
error at a time this sometimes complicates the parser to such an
extent, and produces so many false errors that it may not be very
useful. You'll see this a lot in C++ parsers where the compiler can't
filter out secondary errors caused by an earlier problem.

I'd also say exceptions are fine for what you're doing. I use them
under these circumstances myself.
K

Mar 5 '07 #6
On Mar 5, 6:02 am, "Kirit Sælensminde" <kirit.saelensmi...@gmail.com>
wrote:
On Mar 5, 6:44 am, "mast...@yahoo.com" <mast...@yahoo.comwrote:
The subject line was a bit misleading, you aren't using exceptions asa
control mechanism, which is a bad thing, you are using then to catch an
exceptional condition, which is a good thing.
Thank you Ian.
So even though the exceptional condition I catch is a syntax error in
the file which is getting parsed, this approach is still okay, is it ?
It is convenient for me because of course whenever an exception is
caught the memory is released poperly. So that's why i ended up using
it in that case.

I think it also depends no what you intend to do about it. If the
parser cannot continue or do anything useful then an exception is
probably fine. If it is going to try to complete the parse anyway (and
maybe try to flag other errors) then some other error handling
mechanism is probably more suitable.

Depending on what I'm parsing I may use either mechanism. If the text
to be parsed is generated by a user it is often better to try to
identify many errors at once (like your C++ compiler no doubt does).
If it is expected that the text is generated by a program then it is
entirely appropriate to stop on the first error (as XML parsers must).

Even if it would be convenient for your users to get more than one
error at a time this sometimes complicates the parser to such an
extent, and produces so many false errors that it may not be very
useful. You'll see this a lot in C++ parsers where the compiler can't
filter out secondary errors caused by an earlier problem.

I'd also say exceptions are fine for what you're doing. I use them
under these circumstances myself.

K
These are great explanations. Please ignore my last post in this
thread. I didn't refresh my newsreader.

Thanks
Adrian

Mar 5 '07 #7

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

Similar topics

2
by: greatbooksclassics | last post by:
Open Source DRM? What does everyone think about it? Will Open Source DRM ever catch up to MS DRM? Will DRM ever be integrated into common LAMP applications?...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
17
by: Danny J. Lesandrini | last post by:
The following code works with a standard MDB to navigate to a particluar record (with a DAO recordset, of course) but it's giving me problems in an ADP I'm working on. Dim rs As ADODB.Recordset...
3
by: David Rose | last post by:
I would like to catch the tab change before it occurs. There is a SelectedIndexChanged event to catch the change after it occurs. How do I catch when the user clicks on a tab, but before the...
23
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally...
7
by: Mark Waser | last post by:
Hi all, I'm trying to post multipart/form-data to a web page but seem to have run into a wall. I'm familiar with RFC 1867 and have done this before (with AOLServer and Tcl) but just can't seem...
5
by: Chris Thunell | last post by:
I am looping through a bunch of records in 1 database and putting them into an ado.net datatable I get a key violation when i try to add a record... so what i'm trying to do if that happens, is...
7
by: Sean Kirkpatrick | last post by:
I got caught with my pants down the other day when trying to explain Try...Catch...Finally and things didn't work as I had assumed. Perhaps someone can explain to me the purpose of Finally. I've...
1
by: Anonieko | last post by:
Understanding and Using Exceptions (this is a really long post...only read it if you (a) don't know what try/catch is OR (b) actually write catch(Exception ex) or catch{ }) The first thing I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.