0

I have defined my own expection class:

public class ProduktException extends Exception {

    public ProduktException(String msg){
    //null
    }

    public static void throwProduktNotCreatedException() throws ProduktException {
        throw new ProduktException("Cannot be created!");
    }

    public static void throwProduktNotDeletedException () throws ProduktException {
        throw new ProduktException("Cannot be deleted!");
    }
}

My Problem is I do not know how to throw them when I try:

try {
...
} catch(ProduktNotDeletedException e) {
    e.toString();
}

That does not work... But I want to have these structure! What is wrong?

I appreaciate your answer!!!

UPDATE:

My Problem is, I do not want to create several Exception Klasses I want to have all Exceptions in one class. Is there possibly a solution for that?

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
maximus
  • 11,264
  • 30
  • 93
  • 124
  • What do you mean it does not work? You convert the exception to a string which you immediately throw away. Of course nothing is going to happen. – Cubic Sep 26 '12 at 08:08
  • 2
    Is it `ProduktNotDeletedException` or `ProduktException`? Why do you call toString and then do nothing with it? Why don't you pass the msg along to the super constructor? And what exactly is not working? – Thilo Sep 26 '12 at 08:08
  • Thx for your response!!! The problem is I want to throw f.ex.: a specific ProduktNotDeletedException and not the ProduktException. Therefore, how to do that without making a new class for each exception? – maximus Sep 26 '12 at 08:11

4 Answers4

1

If you need to differentiate between different kinds of exceptions, just create 2 different exceptions, maybe something like:

public class ProduktException extends Exception
{
    public ProduktException(String msg){
    //null
    }
}

Then have:

public class ProduktNotDeletedException extends ProduktException
{
    ....
}

and

public class ProduktNotCreatedException extends ProduktException
{
    ....
}

Then you can catch one or the other, or both.

try {
    ...
} catch(ProduktNotDeletedException e1) {
    e1.toString();
} catch(ProduktNotCreatedException e2) {
    e2.toString();
} 

EDIT:

For a single class what I mean is:

public class ProduktException extends Exception {

    boolean notDeleted;
    boolean notCreated;

    public ProduktException(String msg){
        super(msg);
    }

    public boolean isNotDeleted() {
        return(notDeleted);
    }

    public boolean isNotCreated() {
        return(notCreated);
    }

    public static void throwProduktNotCreatedException() throws ProduktException {
        ProduktException e = new ProduktException("Cannot be created!");
        e.notCreated = true;
        throw e;
    }

    public static void throwProduktNotDeletedException () throws ProduktException {
        ProduktException e = new ProduktException("Cannot be deleted!");
        e.notDeleted = true;
        throw e;
    }
}

Then in your try/catch:

try {
    ...
} catch(ProduktException e) {
    e.toString();
    if(e.isNotCreated()) {
        // do something
    }
    if(e.isNotDeleted()) {
        // do something
    }
}
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • is there probably a way without making different new classes and put everything in one class? – maximus Sep 26 '12 at 08:12
  • @maximus You can create a specific method or a field maybe. like `isNotDeleted()` and `isNotCreated()`, which you can check inside your catch{} block. – Jon Lin Sep 26 '12 at 08:13
  • thx for your answer!!! but I not really understanding what do you mean? Could you give an example? – maximus Sep 26 '12 at 08:15
  • @maximus I've added an example for a specific field. – Jon Lin Sep 26 '12 at 08:27
  • @Jon Lin The problem with this is that he will have to update his Exception class and add a new method every time he wants to create a new exception type. This could add up to quite a few methods if he has many exception types. – Christina Sep 26 '12 at 08:44
  • @Christina Yeah, if there's going to be lots of various types of `ProduktException`, then use an enumeration. Otherwise, I still think it's better to have a superclass, and create subclasses because that makes it so methods that are marked to throw a specific type of `ProduktException` can do so, otherwise it can be marked to throw the superclass `ProduktException` – Jon Lin Sep 26 '12 at 08:48
0

You need to either catch ProduktException, e.g.

try {
  ...
} catch (ProduktException e) {
  e.toString();
}

or declare subtypes, e.g.

public ProduktNotDeletedException extends ProduktException

You'll probably want to pass the message in the constructor up, so add the following in your constructor:

super(msg);
David Grant
  • 13,929
  • 3
  • 57
  • 63
0

The Syntax given below.

class RangeException extends Exception
{
    String msg;

    RangeException()
    {
        msg = new String("Enter a number between 10 and 100");
    }
}

public class MyCustomException
{
    public static void main (String args [])
    {
        try
        {
            int x = 1;

            if (x < 10 || x >100) throw new RangeException();
        }
        catch(RangeException e)
        {
            System.out.println (e);
        }
    }
}
Yogendra Joshi
  • 228
  • 1
  • 8
0

What you could do if you don't want to create multiple subclasses of your ProduktException for each different type of exception you need to throw is to include a code in the exception which will let you know what is wrong. Something like this:

public class ProduktException extends Exception {
    private Code exceptionCode;  
    private String message 

    public ProduktException(Code code, String msg){
        this.message = msg;
        this.exceptionCode = code;
    }  

    //Getters and setters for exceptionCode and message      
}

Code can be an enum so that your application can know that each code corresponds to a specific "problem" (product not created, product not deleted, etc.). You can then throw your exceptions like this

throw new ProduktException(Code.PRODUCT_NOT_CREATED, 
                     "Error while creating product");

And when you catch it you can differentiate based on the code.

catch (ProduktException ex) {
    if (ex.getExceptionCode().equals(Code.PRODUCT_NOT_CREATED)) {
        ...
    }
    else {
        ...
    }
}
Christina
  • 3,562
  • 3
  • 22
  • 32