473,799 Members | 3,185 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1874
ma*****@yahoo.c om 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.c om 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************* ********@t69g20 00...legro ups.com, "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 "exceptiona l" 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.saelensm i...@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
2475
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? (LAMP=Linux/Apache/MYSQL/PHP/Perl/Python/Ruby) Here's Sun's latest initiative in Open Source DRM: DReaM: Royalty-Free, Open Source DRM
121
10186
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 support IDEs are DreamWeaver 8 and Zend PHP Studio. DreamWeaver provides full support for Unicode. However, DreamWeaver is a web editor rather than a PHP IDE. It only supports basic IntelliSense (or code completion) and doesn't have anything...
17
4231
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 Set rs = Me.RecordsetClone rs.Find "=" & lngContractID If Not rs.EOF Then Me.Bookmark = rs.Bookmark I must site the Heisenberb Uncertainty Principal here, as it
3
49442
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 TabPage changes? Is there anything like the TCN_SELCHANGING notification? TIA David Rose
23
3083
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 block. But, I prefer to dim them "on the fly" only if needed (save as much resources as possible). A little further... I may wish to create a sqlcommand and datareader object ONLY if certain conditions are met. But, if I want to clean these up in the...
7
17343
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 to get it to work in Visual Basic. I tried coding it once myself from scratch and then modified a class that I found on a newsgroup (referenced below). Both seem to be doing the same thing and neither works (or rather, they seem to work but the...
5
1248
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 skip that record and move on to the next record... I haven't quite figured out the Try / Catch scenario. Here is my code: Try Me.DataSet11.tblContact.Rows.Add(myNewRow)
7
1732
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 looked at several texts that I have and none of them address this specific point. If I call some method that throws an exception in my routine Foo, sub foo call bar <- throws an exception do something else <- never get here
1
2391
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 look for when evaluating someone's code is a try/catch block. While it isn't a perfect indicator, exception handling is one of the few things that quickly speak about the quality of code. Within seconds you might discover that the code author...
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10470
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10247
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10214
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10023
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9067
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7561
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4135
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 we have to send another system
3
2935
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.