2

In my use case, I am looping across a map and checking whether a particular key is present in a list. If it is present then I have to trow and exception otherwise continue with the execution.

Map<A,B> myMap = new HashMap<A,B>(); //code to populate values in myMap ... ... List<A> myList = new ArrayList<A>(); //code to populate values in myList ... ... for(Map.Entry<A,B> eachElementInMap:myMap.entrySet()){ if(myList.contains(eachElementInMap:myMap.getKey())){ //throwing exception throw new MyCustomizedException("someString"); } } //code continues ... .... 

In the above example, if there are 3 elements in the map(myMap) in which 1 key is present in the list(myList), I want to throw the exception for one and it should continue executing other lines of code for the rest two. Am I using a wrong design to achieve this? Any help or suggestion is appreciated! Thanks

1
  • 2
    Am I using a wrong design to achieve this? A - Yes, maybe you can create a List of Exceptions/errors/whatever and return that to the calling method Commented Jun 18, 2019 at 1:25

4 Answers 4

3

Typically once you throw an exception, you are saying that the current line of execution should terminate, rather than continue. If you want to keep executing code, then maybe hold off on throwing an exception.

boolean fail = false; for (Map.Entry<A,B> eachElementInMap:myMap.entrySet()) { if (myList.contains(eachElementInMap:myMap.getKey())) { // throw an exception later fail = true; } } if (fail) { throw new MyCustomizedException("someString"); } 
Sign up to request clarification or add additional context in comments.

2 Comments

Hi @Tim Biegeleisen, In this case wont it stop execution if fail becomes true?
Yes, the code would keep executing, but it would keep track if the list contained even a single matching element, in which case it would throw an exception at the end. I offer my answer as just a general pattern which might fit what you need.
2

You can also create an exception object at a different location from where you throw it. This idiom will be useful in cases where the exception message is not just "someString", but needs to be constructed from data obtained from the object being iterated over.

Optional<MyCustomizedException> exception = Optional.empty(); for (Map.Entry<A, B> eachElementInMap:myMap.entrySet()) { if (myList.contains(eachElementInMap.getKey())) { // Create an exception object that describes e.g., the missing key(s) // but do not throw it yet. if( exception.isPresent() ) { exception.get().addToDescription( /* Context-sensitive information */ ); } else { exception = Optional.of( new MyCustomizedException( /* Context-sensitive information */)); } } } if( exception.isPresent() ) { throw exception.get(); } 

If the only data stored in the exception is a string, an equivalent effect can be achieved by accumulating problem descriptions in a StringBuilder, but for cases where more interesting data needs to go into the exception object, building as you go might be an option worth considering.

Comments

0

You can split it into two lists,failList and successList. and do it.

This is clearer

failList = myMap.entrySet().stream().filter(p->myList.contains(p.getKey())).collect(Collectors.toList()); successList = myMap.entrySet().stream().filter(p->!myList.contains(p.getKey())).collect(Collectors.toList()); failList.forEach(p -> { // fail code }); successList .forEach(p -> { // success code }); 

Comments

0

why not use if...else instead of try catch ? error just means that's a mistake. if you afraid that makes some mistakes what you don't know. you can use throw error.

anyway, it should not be used when the program is running as you wish

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.