While trying to break text at underscores in typewriter fonts I stumbled
over hyphenat, which offers a great solution for my problem (although I
redeclared \BreakableUnderscore to not use a hyphen).
During my search on how to break the text, I eventually found a stack
exchange answer recommending hyphenat with a disappointing comment it
wouldn't work: https://tex.stackexchange.com/a/290801/51583 .
\documentclass[a4paper]{article}
\usepackage[htt]{hyphenat}
\begin{document}
If an unrecoverable error occurs during the transformation, then a
\texttt{javax.xml.transform.TransformerException} is thrown.
If an unrecoverable error occurs during the transformation, then a
\texttt{javax\_xml\_transform\_TransformerException} is thrown.
If an unrecoverable error occurs during the transformation, then a
\texttt{javax\textbackslash{}xml\textbackslash{}transform\textbackslash{}TransformerException}
is thrown.
If an unrecoverable error occurs during the transformation, then a
\texttt{javax/xml/transform/TransformerException} is thrown.
\end{document}
Interestingly, hyphenat only breaks the second version with the
underscores, contrary to the expectation.
From a user: