943,561 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1890
  • C++ RSS
Sep 1st, 2007
0

Exception Handling

Expand Post »
Hi, I took a course in JAVA and learned about exceptions...I'm not sure how to throw and catch in C++. Can I get some help please???
Similar Threads
Reputation Points: 86
Solved Threads: 2
Junior Poster
venomlash is offline Offline
143 posts
since Oct 2006
Sep 1st, 2007
0

Re: Exception Handling

Since I know little about it, I'll point you here.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 1st, 2007
0

Re: Exception Handling

Here's another tutorial
Last edited by SpS; Sep 1st, 2007 at 11:15 pm.
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Sep 2nd, 2007
0

Re: Exception Handling

One of expecption handlings greatest claims to fame is ability to unroll the stack. Lets suppose you have subroutines "A", "B", "C". Your main code establishes a try block, where in it you call "A" and then "A" calls "B" and "B" calls "C". In "C" there is an error and you throw and expection and it will revert right back to the catch statement in your main module re-establishing the stack as if it had returned normally to that point

Somewhat similiar to the way ON ERROR GOSUB works in VB

So what I try to do is in all my subs and fuctions just throw exceptions and implement appropriate try blocks in the main body of program.

http://relisoft.com has some pretty good tutorials on this already incorportated into full functioning programs.
Last edited by Tight_Coder_Ex; Sep 2nd, 2007 at 10:58 am. Reason: Fixed relisoft link
Reputation Points: 47
Solved Threads: 17
Posting Whiz in Training
Tight_Coder_Ex is offline Offline
215 posts
since Feb 2005
Sep 2nd, 2007
0

Re: Exception Handling

>> I took a course in JAVA and learned about exceptions...I'm not sure how to throw and catch in C++.

the fundamental difference is the use of the 'resource acquisition is initialization' technique in resource management. the c++ philosophy is
a. error handling code must be clearly seperate from normal code
b. you should not have to write error handling code (read no finally scattered all around the place to clean up when exceptions are thrown) except at the place where you attempt to handle the error.
c. the possibility of an error is not deemed to be an error. (exceptions are not checked at compile-time; if exception specifications are not violated at runtime there is no error).
d. the exception mechanism (as the name suggests) is designed to handle exceptional situations; (for example, when you iterate over a sequence, there is nothing exceptional about reaching its end (i would say that never being able to reach the end would be exceptional).
e. exceptions are expensive at runtime (stack unwind is required), but should cause minimum overhead if no exceptional situation occurs. exceptions are the exception rather than the rule.

so your java experience might in some ways be a disadvantage; you should be willing to unlearn a few things.

here are a few links which deal with the conceptual (rather than syntactic) differences between java and c++ exceptions.
http://www.hackcraft.net/raii/
http://en.wikipedia.org/wiki/Resourc...Initialization
http://www.informit.com/articles/art...&seqNum=8&rl=1
http://www.artima.com/intv/modern3.html
http://www.research.att.com/~bs/3rd_safe.pdf
the last link (appendix E) is about exception safety in the c++ standard library; if you are starting out on c++, read section E.6 'implications for library users'.
Last edited by vijayan121; Sep 2nd, 2007 at 2:52 pm.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Reading a Registry Key path from a file
Next Thread in C++ Forum Timeline: meta-library for compile time matrix operations





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC