There can be various causes for this behavior. Has he always been this way or is this a new behavior? If it's a new behavior, it might be worthy of seeing the vet. Keep record of whether this behavior takes place when he is touched in a particular area. Also, worthy of wondering is whether the times he tries to bite is because he is being cornered or restrained in some way that prevents him from walking away.When dogs can't move away, they are likely to become defensive and bite. Is this a puppy? If so, he needs to learn to enjoy being handled. This can be done by petting briefly praising and then feeding a treat, pet/treat, pet/treat, pet/treat for several sessions, gradually increasing the time he is being pet, until he looks forward to being pet because he has asociated with all these good things. He can also be pet briefly before being released to go eat his meal. This method can be used as well with older dogs, but I would recommend seeing a trainer or behavior consultant for safety and correct implementation.