How to return error in springboot and close a Closeable class? When I got some error in springboot and close a closable class is returning 200 OK
don't I need close the Closeable? Is there some way to handle this on springboot?
CSVPrinter csvPrinter;
try{
csvPrinter = new CSVPrinter(httpServletResponse.getWriter() , CSVFormat.DEFAULT);
System.out.println(1/0); // force exception to jump into catch
}catch (Exception e) {
e.printStackTrace();
try { csvPrinter.close(true); } catch (IOException e1) { e1.printStackTrace(); } // has returned 200 OK when closing CSVPrinter
throw new RuntimeException("Error"); // this are been called but was already returned 200
}
I've tried on try-with-resources but no success too
try( final CSVPrinter csvPrinter = new CSVPrinter( httpServletResponse.getWriter() , CSVFormat.DEFAULT) ){
System.out.println(1/0); // // force exception to jump into catch
}catch (Exception e) { // has returned 200 OK
throw new RuntimeException("Error"); // this are been called but was already returned 200
}
I'm inject httpResponse in controller
@GetMapping("/export")
public void exportInCsv(
ExportRequest exportRequest,
HttpServletResponse httpServletResponse // inject httpreponse
){
exportService.writeResponse(exportRequest,httpServletResponse);
}
I have no controller exception handler