Exception

Hello!

I have mad my own exception like this:

package example.nextgen.model;

publicclass ProductDescriptionMissingExceptionextends Exception{

private Throwable cause =null;

public ProductDescriptionMissingException(){

super();

}

public ProductDescriptionMissingException(String message){

super(message);

}

public ProductDescriptionMissingException(String message, Throwable cause){

super(message);

this.cause = cause;

}

public Throwable getCause(){

return cause;

}

publicvoid printStackTrace(java.io.PrintStream ps)

{

super.printStackTrace(ps);

if (cause !=null){

ps.println("Caused by:");

cause.printStackTrace(ps);

}

}

publicvoid printStackTrace(java.io.PrintWriter pw)

{

super.printStackTrace(pw);

if (cause !=null){

pw.println("Caused by:");

cause.printStackTrace(pw);

}

}

}

Then i try to do this:

throw new ProductDescriptionMissingException("Product description does not exist!");

I get the error message:

compile:

[javac] Compiling 16 source files to C:\KTH2\Objektorienterad_design\L3\with

-tests\build\classes

[javac] C:\KTH2\Objektorienterad_design\L3\with-tests\src\main\example\nextg

en\controller\Register.java:54: unreported exception example.nextgen.model.Produ

ctDescriptionMissingException; must be caught or declared to be thrown

[javac] throw new ProductDescriptionMissingException("Product de

scription does not exist!");

[javac]^

[javac] Note: Some input files use unchecked or unsafe operations.

[javac] Note: Recompile with -Xlint:unchecked for details.

[javac] 1 error

BUILD FAILED

I import the exception class in the file where i try to throw it.

What am i doing wrong?

[3186 byte] By [mattias_westerberga] at [2007-11-26 18:54:55]
# 1

> unreported exception example.nextgen.model.ProductDescriptionMissingException; must be caught or declared to be thrown

Which part of the message don't you understand? If you throw an exception that's not extending runtimeException, you either need to add a throws clause to the method's declaration, or you need to catch it (which would be nonsense, usually).

CeciNEstPasUnProgrammeura at 2007-7-9 20:32:30 > top of Java-index,Java Essentials,Java Programming...
# 2
Is a missing product description really that exceptional?
paulcwa at 2007-7-9 20:32:30 > top of Java-index,Java Essentials,Java Programming...
# 3
Maybe not. This is just an programming exercise to learn to make own exception classes.
mattias_westerberga at 2007-7-9 20:32:30 > top of Java-index,Java Essentials,Java Programming...