0

I'm working with netbeans and i created this exception class:

public class VehicleException extends Exception{
    private String matricula;
    private Calendar dataMatricula;
    private ModelVehicle model;

    private int causa;

        public VehicleException(int causa, Object valor) throws Exception {
        this.causa = causa;
        switch (causa) {
            case 1:
                dataMatricula = (Calendar) valor;
                break;
            case 2:
                matricula = (String) valor;
                break;
            case 3:
                model = (ModelVehicle) valor;
                break;

            default:
                throw new Exception("Utilització errònia en construir VehicleException. Causa: " + causa);
        }
    }

         @Override
    public String getMessage() {
        switch (causa) {
            case 1:
                return "dataMatricula erroni: " + dataMatricula + " Ha de contenir valor";
            case 2:
                return "matricula erroni: " + matricula + " Ha de contenir valor";
            case 3:
                return "Model erroni: " + model + " Ha de contenir valor";
            default:
                return "";
        }
    }

And I'm working with this kind of exception in my class "Vehicle" :

public class Vehicle implements Comparable<Vehicle> {

    private String matricula;
    private ModelVehicle model;
    private Calendar dataMatricula;


     public Vehicle(String matricula, ModelVehicle model, Calendar dataMatricula) throws VehicleException {
        setMatricula(matricula);
        setDataMatricula(dataMatricula);
        setModelVehicle(model);


    }


     public String getMatricula(){
         return matricula;
     }

     public ModelVehicle getModelVehicle(){
         return model;
     }
     public Calendar getDataMatricula(){
         return dataMatricula;
     }


      public final void setMatricula(String matricula) throws VehicleException {
        if (matricula == null || matricula.compareTo("") == 0) {
            throw new VehicleException(2,matricula);
        }
        this.matricula = matricula;
    }

      public void setModelVehicle(ModelVehicle model) throws VehicleException{
          if(model == null){
             throw new VehicleException(3,model);
          }
          else {
              this.model = model;
          }
      }

      public void setDataMatricula(Calendar c) throws VehicleException{
          if(c == null){
             throw new VehicleException(1,c);
          }
          this.dataMatricula = c;
      }

The problem cones when i try to compile,in the setter methods i get this message :

error: unreported exception Exception; must be caught or declared to be thrown throw new VehicleException(2,matricula); C:\Users\Ivan\Desktop\Examen isidrer\M03-uf5\Exmaenm03uf5\src\info\infomila\Vehicle.java:55: error: unreported exception Exception; must be caught or declared to be thrown throw new VehicleException(3,model); C:\Users\Ivan\Desktop\Examen isidrer\M03-uf5\Exmaenm03uf5\src\info\infomila\Vehicle.java:64: error: unreported exception Exception; must be caught or declared to be thrown throw new VehicleException(1,c);

I don't quite undestand why this is happening, crearly the setter methods have a "throws VehicleException".

1 Answer 1

5

The constructor for VehicleException can throw an exception itself:

public VehicleException(int causa, Object valor) throws Exception {
                                                 ^^^^^^^^^^^^^^^^

This means your calling scope would need to handle/rethrow that exception from the constructor, or you could choose to suppress the exception rather than throwing it.

default:
    super.addSuppressed(new Exception("Utilització errònia en construir VehicleException. Causa: " + causa));
Sign up to request clarification or add additional context in comments.

3 Comments

From the message passed to the Exception constructor i'd usually tend to throw a RuntimeException here.
@Izruo If they throw a RuntimeException, then the VehicleException would never be seen, so this allows both exceptions to appear in the error log.
My point is that an Exception is normally suppressed when it is caused due to some other Exception being thrown. This one here will be thrown when a programmer uses a wrong constant in his code (which in this case is throwing the Exception). But when it comes to logging, you've got a point here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.