Reputation: 323
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".
Upvotes: 0
Views: 356
Reputation: 33476
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));
Upvotes: 5