473,327 Members | 2,071 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,327 software developers and data experts.

How to catch the exception?

eas
How to catch the following exceptions?

Failure in an assignment operator
Detection of an out-of-bound index in a user-defined array
Range-checked access to a vector

Any comments are appreciated!

Jul 22 '05 #1
3 1860
"eas" <lp****@countrywide.att.net> wrote...
How to catch the following exceptions?
All exceptions are caught by a 'catch' clause after a 'try'
block in which the exception is thrown.
Failure in an assignment operator
What kind of "failure"?
Detection of an out-of-bound index in a user-defined array
No such standard exception, AFAIK. Consult your compiler manual
for a possible compiler-specific one.
Range-checked access to a vector


Do you mean std::vector or some other "vector"? If the standard
one, then there is no specific exception thrown on an attempt to
dereference an invalid iterator. There can be, of course, some
kind of implementation-specific one, but you'll have to look in
the compiler documentation for that.

Victor
Jul 22 '05 #2

"eas" <lp****@countrywide.att.net> wrote in message news:_h*********************@bgtnsc05-news.ops.worldnet.att.net...
How to catch the following exceptions?

Failure in an assignment operator
Detection of an out-of-bound index in a user-defined array
Range-checked access to a vector

Any comments are appreciated!


operator[] on vector doesn't do any range checking.
vector::at does check and throw...

vector<int> v(10);

try {
v.at(12);
} catch (out_of_range) {
cerr << "You fool!\n";
}

Jul 22 '05 #3
eas wrote:
How to catch the following exceptions?
With a catch() block, as Victor said.

Failure in an assignment operator
Assignment operators are source-code constructs, which don't exist at
runtime. So I guess you mean a user-defined operator=(). In that case,
the function would have to throw an exception in order for it to be
caught, and you'd simply catch that exception as you would any other.
Detection of an out-of-bound index in a user-defined array
What do you mean by "user-defined array"? if you are talking about
built-in arrays, there's no standard exception that is thrown - an
out-of-bound access simply invokes undefined behavior.

If you defined an array class, you could check for out-of-bound accesses
and throw whatever exception you want. You would catch this the same way
you catch any other exception.
Range-checked access to a vector


If you use the vector::at() function, an exception will be thrown for an
out-of-range access, as Ron mentioned. operator[] silently invokes
undefined behavior on an out-of-range access.

In both the array and vector cases, you could feasibly create your own
range-checked iterators that throw an exception of your choice.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #4

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

Similar topics

10
by: Gary.Hu | last post by:
I was trying to catch the Arithmetic exception, unsuccessfully. try{ int a = 0, b = 9; b = b / a; }catch(...){ cout << "arithmetic exception was catched!" << endl; } After ran the program,...
7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
5
by: Jacek Dziedzic | last post by:
Hi! In my main() function I have a last-resort exception construct that looks like this: int main() { try { // ... program code }
11
by: Pohihihi | last post by:
I was wondering what is the ill effect of using try catch in the code, both nested and simple big one. e.g. try { \\ whole app code goes here } catch (Exception ee) {}
13
by: Benny | last post by:
Hi, I have something like this: try { // some code } catch // note - i am catching everything now {
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...
3
by: will | last post by:
Hi all. I've got an question about how to catch an exception. In Page_Load, I place a DataGrid, dg1, into edit mode. This will call the method called GenericGridEvent. GenericGridEvent will call...
2
by: Ralph Krausse | last post by:
I created a try/catch/finally but when an expection is thrown, the catch does not handle it... (I know this code is wrong, I want to force the error for this example) try { DataSet ds = new...
11
by: l.woods | last post by:
I want to set up my CATCH for a specific exception, but I really don't know which one of the multitude that it is. I am getting the exception now with Catch ex as Exception but I want to be...
32
by: cj | last post by:
Another wish of mine. I wish there was a way in the Try Catch structure to say if there wasn't an error to do something. Like an else statement. Try Catch Else Finally. Also because I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.