Java how to create a custom Exception to limit the size of a HashMap -
Java how to create a custom Exception to limit the size of a HashMap -
i have method inserts new items hashmap, want limit size of hashmap throw exception when size of hashmap larger specific value, 100, below implementation:
public void addtohashmap(string id, object value) throws hashmapoutofboundexception{     hashmap.put(id,value);     }  private class hashmapoutofboundexception extends exception{     //what should  within class? }       
why not simple:
public void addtohashmap(string id, object value) {     if (hashmap.size()+1 > max_size)       throw new hashmapoutofboundexception();     hashmap.put(id,value);     }    with
public class hashmapoutofboundexception extends runtimeexception {}    from javadoc:
runtimeexception , subclasses unchecked exceptions. unchecked exceptions not need declared in method or constructor's throws clause if can thrown execution of method or constructor , propagate outside method or constructor boundary.
 java exception 
 
Comments
Post a Comment