ajdfhjkshg
ajdfhjkshg

Reputation: 31

exception handling in c++ (in if statement)

I have a function takes integer array and integer as a parameter and find the integer in the array. if there is no such a integer it throws ItemNotFoundException. My func works well but how can use this func in if statement. How can I can understand my func throw exception or not in if statement?

Upvotes: 0

Views: 617

Answers (1)

Aykhan Hagverdili
Aykhan Hagverdili

Reputation: 29945

How can I can understand my func throw exception or not in if statement?

You basically can't. You can catch the exception and then handle the not-found case there but that's cumbersome. Your design could be improved if you did something other than throwing an exception. STL returns an end-iterator, as an example. If you're returning index of the element, you can return -1 or size of the array for instance. The main problem is that you're using exceptions in a places they are not meant to be used.

My suggestion would be to use std::find and not invent the wheel anew.

Upvotes: 1

Related Questions