This may depend on your phone, as phone apps vary.
On my current phone (Samsung, android 12) there is an option in the phone app's settings to "block unknown/private numbers", but that is ambiguous about what "block" means and what "unknown" means. My guess is that "unknown" means "caller ID not available" rather than "not in my contacts", while "block" may or may not divert to voicemail.
Those are the only options I have, but a different phone may have others.
There may also be alternative phone apps that can do this. I know there are such apps that screen junk calls, but have never installed one so don't know what options they have.