Question: While a `HashMap` item can be deleted, or user may input a key that doesn not exist in `HashMap`, what kind of `Try/Catch` block should I add for `.get()` method ?

Here's my code example :

```
public class ProjectModel {

    private int size = 0;
    private Project[] projects = new Project[100];
    private HashMap<Integer, Integer> index = new HashMap<Integer, Integer>();

    public Project create(){

        Project project = new Project();
        int id = project.getId();
        projects[size] = project;
        index.put(id, size);        
        size++;     
        return project;

    }

    public Project select(int id){

        int i = index.get(id); //<----- get method here
        Project project = projects[i];
        return project;

    }

    public void delete(int id){

        int i = index.get(id);      
        projects[i] = null;
        index.remove(id);

    }

}

```

---

additional note :
I ask this because I don't want user select a item that is not exists.
Like there is 5 item, but I try to ProjectModel.select(6);
Whenever HashMap.get() return null, I want to handle it (In my mind it's a Exception to the flow).

Edit :

I have read the comments to the answers, thank you. While I am new to Java, please let me know if I got the idea correct or not, please tell me at the comment.

1. It's ok for return null from a "object selecting function"
2. Instead to throw Exception inside this unit, it is better to do the error handle where I call ProjectModel.select().

Answer: 
On the face of it this statement cannot throw an exception:

```
  int i = index.get(id); //<----- get method here

```

The `index` field won't be `null`, and `get` won't raise an NPE.

But ... even so an NPE can happen. The problem is that if `get(id)` returns `null`, an NPE will be thrown when you attempt to unbox the `null` to assign a value to `i`!

However, the correct solution is to avoid the NPE rather than catching it.

```
 Integer i = index.get(id);
 if (i == null) {
     // deal with this as appropriate
 } else {
     return projects[i];
 }

```
> 
> What Try/Catch block should I use for HashMap.get()?
> 
> 
> 

None. `get()` method does not throw any cached Exception and so you need not have a try catch block to wrap the get call.

Sure you may get a `NPE` or `ConcurrentModificationException` and you may want to have a try catch block for it but general programming practices dictate you should not handle such runtime Exception but rather write code that avoids them like adding a null check.

If all you want to see if entry exists you can call `containsKey()` meythod or check return value of get call itself. 

```
public V get(Object key) {
    if (key == null)
        return getForNullKey();
    Entry<K,V> entry = getEntry(key);

    return null == entry ? null : entry.getValue();
}

```

You should get null as return value if Entry does not exist.