What if a lecturer has to use the bathroom while on stage?
Ideally, you use the bathroom before lecturing which should prevent you from needing the bathroom during the lecuture. In cases where you are unwell, I find it best to let the class know ahead of time to tell them you are unwell and might need to suddenly excuse yourself, but that you will be back.

Regardless of if you have told the students in advance, if you must excuse yourself, do it politely and tell them you will be back in a few moments.