Embed presentation
Download as PDF, PPTX








![Create new exception
public abstract class BaseLocalizedException extends RuntimeException {
public String getLocalizedMessageKey() {
return "exception.objectNotFound"
}
public abstract Object[] getLocalizedMessageArguments();
}
public class BusinessObjectNotFoundException extends BaseLocalizedException {
private String objectType;
public String getLocalizedMessageKey() {
return "exception.objectNotFound"
}
public Object[] getLocalizedMessageArguments() {
return new Object[] {objectType};
}
}](https://image.slidesharecdn.com/exceptionhandling-160620200759/75/Exception-handling-9-2048.jpg)








![Logger - SLF4J + logback / log4j / java.util.
logging
SLF4J - Simple Logging Facade for Java - allows end user to plug-in the desired
logging framework at the deployment time.
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HelloWorld {
public static void main(String[] args) {
Logger logger = LoggerFactory.getLogger(HelloWorld.class);
logger.info("Hello World");
}
}](https://image.slidesharecdn.com/exceptionhandling-160620200759/75/Exception-handling-18-2048.jpg)
![Comparison of logger frameworks
Log4j - logging library for Java [2000-2002] ( no info about bugs )
Logback - Logback is intended as a successor to the popular log4j project,
picking up where log4j leaves off. [2006-now ] ( 324 unresolved )
Log4j 2 - Apache Log4j 2 is an upgrade to Log4j that provides significant
improvements over its predecessor, Log4j 1.x, and provides many of the
improvements available in Logback while fixing some inherent problems in
Logback's architecture. - [2012-now] (276 unresolved issues)](https://image.slidesharecdn.com/exceptionhandling-160620200759/75/Exception-handling-19-2048.jpg)


The document discusses best practices for exception handling in Java applications. It recommends that exceptions should only be used for exceptional situations, be properly logged, and result in appropriate error responses. Business exceptions should be thrown for invalid user behavior, while technical exceptions occurring internally should be wrapped in business exceptions. Exceptions should have clear, descriptive names and result in the proper HTTP status codes. The document also provides examples of implementing localized exceptions, handling exceptions globally or at the controller level, and using SLF4J with Logback for logging.
Ania Pietras introduces the topic of exception handling in programming.
Guidelines for using exceptions, types (Business vs Technical), and naming conventions for exceptions.
Overview of HTTP status codes relevant to error handling including 400, 404, and 500.
Describes the process between the global exception handler, service layer, and response to the client.
Setup for internationalization of error messages using properties files and Spring configuration.
Implementation of a base localized exception and custom exceptions extending the base class.
Detailing the handling of technical and business exceptions within the service layer.
Different strategies for handling exceptions: per Exception, per Controller, and globally.
Using the @ResponseStatus annotation to automatically return specific HTTP statuses for exceptions.
Global handling of exceptions using @ControllerAdvice and logging practices during exceptions.
Best practices for logging exceptions, including the use of various logging frameworks.
Comparison of different Java logging frameworks: Log4j, Logback, and Log4j 2.
Resources for deeper understanding and best practices in exception handling in Java.








![Create new exception
public abstract class BaseLocalizedException extends RuntimeException {
public String getLocalizedMessageKey() {
return "exception.objectNotFound"
}
public abstract Object[] getLocalizedMessageArguments();
}
public class BusinessObjectNotFoundException extends BaseLocalizedException {
private String objectType;
public String getLocalizedMessageKey() {
return "exception.objectNotFound"
}
public Object[] getLocalizedMessageArguments() {
return new Object[] {objectType};
}
}](https://image.slidesharecdn.com/exceptionhandling-160620200759/75/Exception-handling-9-2048.jpg)








![Logger - SLF4J + logback / log4j / java.util.
logging
SLF4J - Simple Logging Facade for Java - allows end user to plug-in the desired
logging framework at the deployment time.
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HelloWorld {
public static void main(String[] args) {
Logger logger = LoggerFactory.getLogger(HelloWorld.class);
logger.info("Hello World");
}
}](https://image.slidesharecdn.com/exceptionhandling-160620200759/75/Exception-handling-18-2048.jpg)
![Comparison of logger frameworks
Log4j - logging library for Java [2000-2002] ( no info about bugs )
Logback - Logback is intended as a successor to the popular log4j project,
picking up where log4j leaves off. [2006-now ] ( 324 unresolved )
Log4j 2 - Apache Log4j 2 is an upgrade to Log4j that provides significant
improvements over its predecessor, Log4j 1.x, and provides many of the
improvements available in Logback while fixing some inherent problems in
Logback's architecture. - [2012-now] (276 unresolved issues)](https://image.slidesharecdn.com/exceptionhandling-160620200759/75/Exception-handling-19-2048.jpg)
