eclipse 抛出我自己的异常?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12597235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 19:04:10  来源:igfitidea点击:

Throw my own exceptions?

javaeclipseexceptionexception-handling

提问by maximus

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?

我的问题是,我不想创建几个 Exception Klasses 我想在一个类中包含所有 Exceptions。有没有可能的解决方案?

回答by Jon Lin

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

如果您需要区分不同类型的异常,只需创建 2 个不同的异常,可能类似于:

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 中:

try {
    ...
} catch(ProduktException e) {
    e.toString();
    if(e.isNotCreated()) {
        // do something
    }
    if(e.isNotDeleted()) {
        // do something
    }
}

回答by Yogendra Joshi

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);
        }
    }
}

回答by Christina

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:

如果您不想为需要抛出的每种不同类型的异常创建 ProduktException 的多个子类,您可以做的是在异常中包含一个代码,让您知道什么是错误的。像这样的东西:

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 {
        ...
    }
}

回答by David Grant

You need to either catch ProduktException, e.g.

你需要要么 catch ProduktException,例如

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);