In Java itself, there is no built‑in “application property” that automatically makes exceptions show their messages — exceptions always have a getMessage() method, and whether they are shown depends on how your application logs or prints them.
However, in Java frameworks (like Spring Boot, Jakarta EE, Micronaut, etc.), there are configuration properties that control whether exception messages are shown in responses or logs.
Spring Boot Example
If you are using Spring Boot, the property to show exception messages in HTTP error responses is:
Properties
server.error.include-message=always
Options:
never(default) → Never include the exception message in the response.always→ Always include it.on_param→ Include it only if a specific request parameter is present.
You can also control stack traces:
Properties
server.error.include-stacktrace=always
Jakarta EE / Plain Java
In plain Java (without a framework), you control exception messages manually:
Java
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage()); // Shows the message
e.printStackTrace(); // Shows full stack trace
}
✅ Summary
- Plain Java → No property; you must explicitly log or print exception messages.
- Spring Boot → Use
server.error.include-messageinapplication.propertiesorapplication.yml. - Other frameworks → Similar config keys exist, but names differ.
If you want, I can give you a full Spring Boot example that shows exception messages in API responses.
Do you want me to prepare that?
0 Comments