How do I avoid errors when retrieving a component for a GameObject that does not have such a component?

I was doing some raycasting so I could interact with objects in my scene. If I happen to click on some of the background objects I get an error like

MissingComponentException: There is no 'Rigidbody' attached to the "fountain" game object, but a script is trying to access it.

probably because my code just calls

rb = hit.collider.gameobject.GetComponent<Rigidbody>();

This is perfectly sensible error message. Since many objects in the scene have no business having a Rigidbody component I think it silly to add one just to suppress this error. How do I test for the presence of a component on the object to avoid triggering the error?

It turns out a better error message would have prevented confusion (Unity was too clever by half). The problem is not that I tried to access GetComponent<Rigidbody>() . The problem is that I did not check it for null before I used it.

I solved my problem by coding

rb = hit.collider.gameobject.GetComponent<Rigidbody>();
if (null != rb) {
    // remainder of logic