Surface ships are very noisy so submarines listen for other ships in the area before surfacing. They have dead zones in their sonar coverage, but can usually deal with that by turning to listen in those dead zones before surfacing. Nevertheless, there are occasional accidents.