My ExceptionHandler is not recognised by the program

42 Views Asked by At

My Compiler shows an unhandled exception notice even though I wrote an ExceptionHandler class.

ExceptionHandler

@ControllerAdvice
public class KExceptionHandler {
    
    @ExceptionHandler(value= {PflichtfeldIsNullException.class})

    public ResponseEntity<Object> handlePflichtfeldIsNullException (PflichtfeldIsNullException pfis) { 
        
        KException kException = new KException (pfis.getLocalizedMessage(), pfis.getCause(), HttpStatus.NOT_FOUND);
        
    return new ResponseEntity<>(kException, HttpStatus.NOT_FOUND);
    }

}

ExceptionClass

public class KException {

    private final String message;
    private final Throwable throwable;
    private final HttpStatus httpStatus;

    public KException(String message, Throwable throwable, HttpStatus httpStatus) {
        super();
        this.message = message;
        this.throwable = throwable;
        this.httpStatus = httpStatus;
    }

    public String getMessage() {
        return message;
    }

    public Throwable getThrowable() {
        return throwable;
    }

    public HttpStatus getHttpStatus() {
        return httpStatus;
    }
}

Specific ExceptionClass

public class PflichtfeldIsNullException extends Exception {
    
    public PflichtfeldIsNullException(String message) { 
        super(message); 
    }
}

Controller

@RestController
@RequestMapping("/kobject")
public class ControllerK {

    @Autowired
    ServiceImpK serviceImpK;

    public ControllerK(ServiceImpK serviceImpK) {
        super();
        this.serviceImpK = serviceImpK;
    }
    
    @PostMapping
    public String createK(@RequestBody List<KObject> dtos)
    { 
//this line gets the Error "unhandeled PflichtfeldIsNullException"
        return serviceImpK.createK(dtos); 
    }

}

The structure of my packages

-- demo (SpringbootApplication with @SpringBootApplication)
   |-- controler (Exceptions are thrown up here)
   |-- exception (Exception Class, Specific Exception Class and ExceptionHandler are here)
   |-- model
   |-- repository 
   |-- service

Things I tried

  • The same ExceptionHandler workes in a DemoProgramm with way less Code
  • using throws to throw the Exceptions into a @Controller (original classes where the exceptions are thrown are @Component)
  • not using throws
1

There are 1 best solutions below

2
On BEST ANSWER
//this line gets the Error "unhandeled PflichtfeldIsNullException"

It's happening because you created custom checked exception so you need to handle it manually with try and catch.

@PostMapping
public String createK(@RequestBody List<KObject> dtos) { 
    try {
        return serviceImpK.createK(dtos);
    } catch (PflichtfeldIsNullException e) {
        // Handle exception
    }
}

Alternatively, you can create a custom unchecked exception by extending RuntimeException

    public class PflichtfeldIsNullException extends RuntimeException {
    
    public PflichtfeldIsNullException(String message) { 
        super(message); 
    }
}

I would also recommend changing ControllerAdvice to RestControllerAdvice since your application functions as a REST API. Additionally, using an unchecked custom exception by extending RuntimeException will resolve your issue and make the code in your controller cleaner.