0% found this document useful (0 votes)
2 views1 page

Exception Handling in Java APIs

The document outlines exception handling methods in a Java application, specifically for IllegalArgumentException and NoSuchElementException. It includes details on creating ProblemDetail objects for error responses and configuring server error settings. Additionally, it demonstrates how to handle exceptions when retrieving and mapping data using a service and model mapper.

Uploaded by

matheustopred
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

Exception Handling in Java APIs

The document outlines exception handling methods in a Java application, specifically for IllegalArgumentException and NoSuchElementException. It includes details on creating ProblemDetail objects for error responses and configuring server error settings. Additionally, it demonstrates how to handle exceptions when retrieving and mapping data using a service and model mapper.

Uploaded by

matheustopred
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

@ExceptionHandler(IllegalArgumentException.

class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ResponseEntity<?>
handleIllegalArgumentException(IllegalArgumentException exception,
WebRequest request) {
ProblemDetail pd = ProblemDetail
.forStatusAndDetail(HttpStatus.INTERNAL_SERVER_ERROR, "Ocorreu
um erro: '"
+ [Link]());
[Link]([Link]("[Link]
server-error"));
[Link]("Erro Interno");
[Link]("hostname", "localhost");
return [Link](500).body(pd);
}

[Link]-message=always
[Link]-binding-errors=always
[Link]-stacktrace=on-param
[Link]-exception=false

PerfilResumidoDto perfilDto = null;


try {
perfilDto = [Link](id);
}catch(IllegalArgumentException e) {
throw new IllegalArgumentException("Ocorreu uma exceção: " + e);
}

try {
perfilDto = [Link](perfil, [Link]);
}catch(IllegalArgumentException e) {
throw new IllegalArgumentException("Ocorreu uma exceção: ", e);
}

@ExceptionHandler([Link])
ProblemDetail handleNoSuchElementException(NoSuchElementException e) {
ProblemDetail problemDetail =
[Link](HttpStatus.NOT_FOUND, [Link]());

[Link]("Recurso Não Encontrado");


[Link]([Link]("[Link]
found"));
return problemDetail;
}

You might also like