Regarding fasts that a person is not commanded in, there is a dispute in the Gemara (11b): One says anyone who sits in a fast is called "holy." And one says he is called a "sinner." And both are the words of the living God. For certainly, if he is able to endure the fast and does not have through this a neglect of Torah study—he is called "holy," for through this his iniquities are purged. And if not, that he is not healthy and strong—he is called a "sinner." And all this is regarding one who fasts by way of asceticism. But one who is a sinner—certainly he must fast for his sins, and weep and confess before Hashem, and return and be healed; unless he will come through the fast to a state of danger, for then it is forbidden for him to fast. And so too if he is a great talmid chakham—it is better to minimize fasts, and he should increase in the study of Torah. But without this—a talmid chakham is not permitted to sit in a fast, because he diminishes the work of Heaven. And all the more so teachers of children, for there are two factors: neglect of the Torah study of the children of the house of their teacher, and he robs the employers. And so too any laborer is not permitted to increase in fasting, because he diminishes the work of the homeowner. However, regarding public fasts—talmidei chakhamim and teachers and laborers are obligated to fast, and it is forbidden to separate oneself from the community. And with this understanding they accepted them. And all those occupied with the needs of the public—their law is like teachers. And regarding the matter of fasting, there is the law of a talmid chakham even in this time. And a minor boy and a minor girl when they take upon themselves a fast—their law is like vows, and it was explained in Yoreh Deah siman 234, see there.
בתעניתים שאין האדם מצווה יש פלוגתא בגמרא (יא ב): חד אמר כל היושב בתענית נקרא "קדוש". וחד אמר נקרא "חוטא". ואלו ואלו דברי אלקים חיים. דוודאי אם יכול לסבול התענית ואין לו על ידי זה ביטול תורה – נקרא "קדוש", שמתוך כך מתמרקין עונותיו. ואם לאו, שאינו בריא וחזק – נקרא "חוטא". וכל זה במי שמתענה דרך פרישות. אבל מי שהוא חוטא – בוודאי יש לו להתענות על חטאיו, ולבכות ולהתוודות לפני ה', ושב ורפא לו; אלא אם כן יבוא על ידי התענית לידי סכנה, דאז אסור לו להתענות. וכן אם הוא תלמיד חכם גדול – טוב יותר למעט בתעניתים, וירבה בלימוד התורה. אבל בלא זה – אין תלמיד חכם רשאי לישב בתענית, מפני שממעט במלאכת שמים. וכל שכן מלמדי תנוקות, דאיכא תרתי: ביטול תורה של תינוקות של בית רבן, וגוזל את הבעלים. וכן כל פועל אינו רשאי להרבות בתענית, מפני שממעט מלאכתו של בעל הבית. אמנם תעניתי הציבור – חייבים תלמידי החכמים והמלמדים והפועלים להתענות, ואסור להפריש את עצמו מן הציבור. ועל דעת כן קבלום. וכל העוסקים בצרכי רבים – דינם כמלמדים. ולעניין תענית יש דין תלמיד חכם גם בזמן הזה. וקטן וקטנה כשמקבלים עליהם תענית – דינם כמו נדרים, ונתבאר ביורה דעה סימן רלד, עיין שם.