Forum Stats

  • 3,854,326 Users
  • 2,264,352 Discussions
  • 7,905,649 Comments

Discussions

RuntimeException

EdChouaffe
EdChouaffe Member Posts: 125
edited Mar 25, 2011 1:09PM in Java Desktop Applications
Hi Guys.

Is there any good reasons why an RuntimeException is not thrown although it should?
Look at the following code snippset:
Note: colConstraints is a HashMap object.
Set<String> newValueSet = .... (here is the object initialized with some values)
for (String key : newValuesSet) {
				if (colConstraints.containsKey(key)) {
					System.out.println(colConstraints + "  " + key);
					throw new RuntimeException(
							"SQLQueryBuilder.UpdateQuery: Misplacement " + key
									+ " cannot be a primary or foreign key");
				}
			}
I wonder why I dont get any error thrown even though there are cases where key is contained within colConstraints.
Best regrads.

Edmond

Answers

  • 796440
    796440 Member Posts: 19,179 Gold Trophy
    edited Mar 25, 2011 1:09PM
    EdChouaffe wrote:
    Hi Guys.

    Is there any good reasons why an RuntimeException is not thrown although it should?
    It's not thrown because it shouldn't be. You need to check your assumptions.
    Look at the following code snippset:
    Note: colConstraints is a HashMap object.
    Set<String> newValueSet = .... (here is the object initialized with some values)
    for (String key : newValuesSet) {
    				if (colConstraints.containsKey(key)) {
    					System.out.println(colConstraints + "  " + key);
    					throw new RuntimeException(
    							"SQLQueryBuilder.UpdateQuery: Misplacement " + key
    									+ " cannot be a primary or foreign key");
    				}
    			}
    I wonder why I dont get any error thrown even though there are cases where key is contained within colConstraints.
    colConstraints doesn't contain any of those keys. If it did, you would get the printout, and the exception would be thrown. Perhaps you didn't put what you though you did into colConstraints. Perhaps colConstraints contains keys of some type other than String. Perhaps you are getting the exception and smothering it elsewhere. Impossible to say with so little information.

    However, I know for certain that containsKey() does work, and the if statement does work. So if the exception is not getting thrown, then colConstraints definitely does not contain that key.
This discussion has been closed.