A response to protect an organism from a directly detected or perceived external threat from a nematode or nematodes, which results in restriction of damage to the organism attacked or prevention/recovery from the infection caused by the attack.