Opaa boa noite tudo bem? Existe algumas formas pra fazer isso, mas o mais comum é criar um arquivo de log que registre as informações relevantes sobre a exceção.
importe as classes necessarias
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
int total = 0;
try {
    System.out.println(total = 1 / 0);
} catch (Exception e) {
    // Crie um arquivo de log (por exemplo, "error.log") e abra um BufferedWriter para escrever nele.
    try (BufferedWriter writer = new BufferedWriter(new FileWriter("error.log", true))) {
        // Obtenha informações da exceção, como mensagem, rastreamento de pilha, data e hora.
        String exceptionInfo = String.format("Exception at %tF %tT%n%s%n%s%n",
                new java.util.Date(), new java.util.Date(), e.getMessage(), getStackTrace(e));
        // Escreva as informações da exceção no arquivo de log.
        writer.write(exceptionInfo);
    } catch (IOException ex) {
        // Trate qualquer exceção que possa ocorrer ao escrever no arquivo de log.
        ex.printStackTrace();
    }
    // Lance uma nova exceção (ou rethrow a exceção original, se desejar) para manter o fluxo de controle.
    throw new RuntimeException(e);
}
// ...
// Método auxiliar para obter o rastreamento de pilha como uma String
public static String getStackTrace(Throwable throwable) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    throwable.printStackTrace(pw);
    return sw.toString();
}