title: Best way to add the custom exception for the spring boot code
body: <p>How to display the appropriate error message when some exception occurs.</p>
  
  <p>Suppose during GET methods if the data is not found, it should display the custom exception message.</p>
  
  <p>Similarly if we are trying to delete the data which is not available.</p>
  
  <p><strong>Car.java</strong></p>
  
  <pre><code>package com.car_rental_project.car_project;
  import javax.persistence.Entity;
  import javax.persistence.Id;
  @Entity
  
  public class Car {
  
      @Id
      private String id;
      private String modelname;
      private String type;
      private String year_of_registration;
      private String seating_capacity;
      private String cost_per_day;
      private String milleage;
      private String pincode;
      private String contact_number;
      private String email;
  
      public Car() {
  
      }
  
      public Car(String id, String modelname, String type, String year_of_registration, String seating_capacity,String cost_per_day, String milleage, String pincode, String contact_number, String email) {
          super();
          this.id = id;
          this.modelname = modelname;
          this.type = type;
          this.year_of_registration = year_of_registration;
          this.seating_capacity = seating_capacity;
          this.cost_per_day = cost_per_day;
          this.milleage = milleage;
          this.pincode = pincode;
          this.contact_number = contact_number;
          this.email = email;
      }
  
      public String getId() {
          return id;
      }
  
      public void setId(String id) {
          this.id = id;
      }
  
      public String getModelname() {
          return modelname;
      }
  
      public void setModelname(String modelname) {
          this.modelname = modelname;
      }
  
      public String getType() {
          return type;
      }
  
      public void setType(String type) {
          this.type = type;
      }
  
      public String getYear_of_registration() {
          return year_of_registration;
      }
  
      public void setYear_of_registration(String year_of_registration) {
          this.year_of_registration = year_of_registration;
      }
  
      public String getSeating_capacity() {
          return seating_capacity;
      }
  
      public void setSeating_capacity(String seating_capacity) {
          this.seating_capacity = seating_capacity;
      }
  
      public String getCost_per_day() {
          return cost_per_day;
      }
  
      public void setCost_per_day(String cost_per_day) {
          this.cost_per_day = cost_per_day;
      }
  
      public String getMilleage() {
          return milleage;
      }
  
      public void setMilleage(String milleage) {
          this.milleage = milleage;
      }
  
      public String getPincode() {
          return pincode;
      }
  
      public void setPincode(String pincode) {
          this.pincode = pincode;
      }
  
      public String getContact_number() {
          return contact_number;
      }
  
      public void setContact_number(String contact_number) {
          this.contact_number = contact_number;
      }
  
      public String getEmail() {
          return email;
      }
  
      public void setEmail(String email) {
          this.email = email;
      }
  }
  </code></pre>
  
  <p><strong>CarService.java</strong></p>
  
  <pre><code>package com.car_rental_project.car_project;
  
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.List;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  
  @Service
  public class CarService {
  
      @Autowired
      private CarRepository CarRepository;
  
      public List&lt;Car&gt; getAllCars() {
          return (List&lt;Car&gt;) CarRepository.findAll();
      }
  
      public Car getCar(String id) {
          return (Car) CarRepository.findOne(id);
  
      }
  
      public void addCar(Car car) {
          this.CarRepository.save(car);
      }
  
      public void updateCar(String id, Car car) {
          this.CarRepository.save(car);
      }
  
      public void deleteCar(String id) {
          this.CarRepository.delete(id);;
      }
  }
  </code></pre>
  
  <p><strong>CarController.java</strong></p>
  
  <pre><code>package com.car_rental_project.car_project;
  
  import java.util.List;    
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.web.bind.annotation.DeleteMapping;
  import org.springframework.web.bind.annotation.PathVariable;
  import org.springframework.web.bind.annotation.PostMapping;
  import org.springframework.web.bind.annotation.PutMapping;
  import org.springframework.web.bind.annotation.RequestBody;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.bind.annotation.RestController;
  
  @RestController
  public class CarController {
  
      @Autowired
      private CarService carService;
  
      @RequestMapping("/car")
      public List&lt;Car&gt; getAllCars() {
          return carService.getAllCars();
      }
  
      @RequestMapping("/car/{id}")
      public Car getCar(@PathVariable String id) {
          return carService.getCar(id);
              }
  
      //@PostMapping("/car")
      @RequestMapping(method=RequestMethod.POST, value="/car")
      public String addCar(@RequestBody Car car) {
          carService.addCar(car);
          String response = "{\"success\": true, \"message\": Car has been added successfully.}";
          return response;
      }
  
      //@RequestMapping(method=RequestMethod.PUT, value="/car/{id}")
      @PutMapping("/car/{id}")
      public String updateCar(@RequestBody Car car, @PathVariable String id) {
          carService.updateCar(id, car);
          String response = "{\"success\": true, \"message\": Car has been updated successfully.}";
          return response;
      }
  
      //@RequestMapping(method=RequestMethod.DELETE, value="/topics/{id}")
      @DeleteMapping("/car/{id}")
      public String deleteCar(@PathVariable String id) {
          carService.deleteCar(id);
          String response = "{\"success\": true, \"message\": Car has been deleted successfully.}";
          return response;
      }
  }
  </code></pre>