0

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.