17- Creating Custom Exception

 

In case the resource or information you are looking for does not exists then I want my application to throw a Custom Exception

Creating Custom Exception

springboot-backendd --> src/main/java --> net.javaguides.springboot.exception -->ResourceNotFoundException.java

package net.javaguides.springboot.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(value=HttpStatus.NOT_FOUND)
public class ResourceNotFound extends RuntimeException {
   
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private String resourceName;
    private String fieldName;
    private Object fieldValue;
   
    public ResourceNotFound(String resourceName, String fieldName, Object fieldValue) {
        super(String.format("%s not found with %s:'%s'",resourceName,fieldName,fieldValue));
        this.resourceName = resourceName;
        this.fieldName = fieldName;
        this.fieldValue = fieldValue;
    }
    public String getResourceName() {
        return resourceName;
    }
    public String getFieldName() {
        return fieldName;
    }
     public Object getFieldValue() {
        return fieldValue;
    }
}

Comments

Popular posts from this blog

INDEX OF Zeek Spring Boot