我可以在一个类文件中定义java中的多个自定义异常并通过方法调用它们吗?

我可以在一个类文件中定义java中的多个自定义异常并通过方法调用它们吗?,java,exception,error-handling,try-catch,throws,Java,Exception,Error Handling,Try Catch,Throws,我试图通过自定义异常进入异常处理 我正在创建CustomExceptions类并扩展Exception,如下所示: public class CustomExceptions extends Exception{ public CustomExceptions (String s) { super(s); } 但是,我不必为我想要的每个自定义异常创建多个文件,也不必膨胀我的主类文件,我希望将所有自定义异常都放在这个类中,并通过一个方法调用它们 假设我想处理两种情

我试图通过自定义异常进入异常处理

我正在创建CustomExceptions类并扩展Exception,如下所示:

public class CustomExceptions extends Exception{
    public CustomExceptions (String s) {
        super(s);
    }
但是,我不必为我想要的每个自定义异常创建多个文件,也不必膨胀我的主类文件,我希望将所有自定义异常都放在这个类中,并通过一个方法调用它们

假设我想处理两种情况:当用户试图输入座位预订,但座位已经有人坐,以及当用户试图为年龄范围以外的人提供机票时

我可以在CustomExceptions类中创建两个方法来调用向其传递自定义消息的构造函数吗

    public void seatTaken(String s) {
        String s = "The seat is taken, please choose a new one";
        CustomExceptions(s);

    }

    public void notOldEnough(String s) {
      String s = "User is not old enough for this movie.";
      CustomExceptions(s)

    }
}

这样行吗?还是我必须创建多个自定义异常文件?

通常,自定义异常应该在顶层定义。因为,几乎所有情况下,这些异常都是包或模块接口的一部分

如果用户看不到它们,那么它们将如何单独捕获它们?如果您不想单独捕获它们,那么为什么需要单独的类呢

但是,如果必须,可以将它们包含到需要它们的类中:

public class SeatReservationSystem {
    public static class ReservationFailedException {
        ... constructors taking a message ...
    }

    public static class SeatTakenException extends ReservationFailedException {
        ... constructors taking a message ...
    }

    public static class OutsideAgeException extends ReservationFailedException  {
        ... constructors taking a message ...
    }

    ....
}
之后,您可以创建任何根据需要返回它们的方法。不要创建抛出它们的方法,因为编译器不会将它们视为所处块的出口点,并且会出现奇怪的情况

下面是一些代码来说明我的意思:

// wrong
public static void throwRuntimeException() throws RuntimeException {
    throw new RuntimeException();
}

// correct, but dangerous
public static RuntimeException createRuntimeException() {
    return new RuntimeException();
}

public static void main(String[] args) {
    String initializeMeOrThrowException;
    if (new Random().nextBoolean()) {
        // compiler doesn't recognize that the method always throws an exception 
        throwRuntimeException();

        // this the compiler can understand, there is an explicit throw here:
        // throw createRuntimeException();

        // but this is the pitfall, it doesn't do anything:
        // createRuntimeException();
    } else {
        initializeMeOrThrowException = "Initialized!";
    }

    // Compiler error for throwRuntimeException and createRuntimeException without throws:
    // "The local variable initializeMeOrThrowException may not have been initialized"
    System.out.println(initializeMeOrThrowException); 
}
然而,经验告诉我,我忘记了throw createException的throws语句。。。;方法,而愚蠢的编译器没有警告我这一点,即使没有它,语句也毫无用处。所以我尽量不使用任何一种


请注意,我不确定您是否应该为此使用异常。如果您的系统是预订系统,那么拒绝购票也不例外。返回ReservationResult更有意义。

通常,自定义异常应该在顶层定义。因为,几乎所有情况下,这些异常都是包或模块接口的一部分

如果用户看不到它们,那么它们将如何单独捕获它们?如果您不想单独捕获它们,那么为什么需要单独的类呢

但是,如果必须,可以将它们包含到需要它们的类中:

public class SeatReservationSystem {
    public static class ReservationFailedException {
        ... constructors taking a message ...
    }

    public static class SeatTakenException extends ReservationFailedException {
        ... constructors taking a message ...
    }

    public static class OutsideAgeException extends ReservationFailedException  {
        ... constructors taking a message ...
    }

    ....
}
之后,您可以创建任何根据需要返回它们的方法。不要创建抛出它们的方法,因为编译器不会将它们视为所处块的出口点,并且会出现奇怪的情况

下面是一些代码来说明我的意思:

// wrong
public static void throwRuntimeException() throws RuntimeException {
    throw new RuntimeException();
}

// correct, but dangerous
public static RuntimeException createRuntimeException() {
    return new RuntimeException();
}

public static void main(String[] args) {
    String initializeMeOrThrowException;
    if (new Random().nextBoolean()) {
        // compiler doesn't recognize that the method always throws an exception 
        throwRuntimeException();

        // this the compiler can understand, there is an explicit throw here:
        // throw createRuntimeException();

        // but this is the pitfall, it doesn't do anything:
        // createRuntimeException();
    } else {
        initializeMeOrThrowException = "Initialized!";
    }

    // Compiler error for throwRuntimeException and createRuntimeException without throws:
    // "The local variable initializeMeOrThrowException may not have been initialized"
    System.out.println(initializeMeOrThrowException); 
}
然而,经验告诉我,我忘记了throw createException的throws语句。。。;方法,而愚蠢的编译器没有警告我这一点,即使没有它,语句也毫无用处。所以我尽量不使用任何一种


请注意,我不确定您是否应该为此使用异常。如果您的系统是预订系统,那么拒绝购票也不例外。返回ReservationResult更有意义。

抛出新的CustomExceptions;-除非出于某种原因希望抛出另一种类型的CustomException,否则不应该需要多个CustomExceptions类。您希望在自定义异常中提供什么方法?谢谢,您的评论让我意识到创建多个自定义异常是多么愚蠢。我的印象是他们必须适应特定的情况,但我刚刚意识到我可以在try块期间将消息作为参数传递。为什么要这样做?重新考虑为什么要使用异常以及为什么要使用自定义异常。应该清楚的是,单个自定义异常可以同时用于以下两个目的:新的自定义异常;-除非出于某种原因希望抛出另一种类型的CustomException,否则不应该需要多个CustomExceptions类。您希望在自定义异常中提供什么方法?谢谢,您的评论让我意识到创建多个自定义异常是多么愚蠢。我的印象是他们必须适应特定的情况,但我刚刚意识到我可以在try块期间将消息作为参数传递。为什么要这样做?重新考虑为什么要使用异常以及为什么要使用自定义异常。应该清楚的是,一个自定义异常可以同时满足这两个目的