简体   繁体   English

Java中的每个自定义异常都需要自己的类吗?

[英]Does each custom exception in Java require its own class..?

I'm new to Java custom exceptions. 我是Java自定义异常的新手。 I see many examples of how to create them, but there are two questions I cannot find answers to: 我看到了许多有关如何创建它们的示例,但是有两个问题我找不到答案:

1) Can a custom exception be defined within the object class it's intended for, or must it be in its own class file? 1)是否可以在要使用的对象类中定义自定义异常,还是必须在其自己的类文件中?

2) Regardless of location, can multiple custom exceptions be defined within a single class file, or does every custom exception require its own class file? 2)无论位置如何,可以在一个类文件中定义多个自定义异常,还是每个自定义异常都需要自己的类文件?

Some code snippets would be greatly appreciated. 一些代码片段将不胜感激。

Thanks. 谢谢。

Exceptions are classes, so obviously they each need their own class. 异常类,因此很显然它们每个都需要自己的类。 It can be a normal class, inner class or a nested class as usual. 它可以像往常一样是普通类,内部类或嵌套类。

As exception are classes so, some code snippet are given below: 作为类的例外,下面提供了一些代码片段:

Java Custom Exception: Java自定义异常:

InvalidAgeException.java InvalidAgeException.java

class InvalidAgeException extends Exception{  
 InvalidAgeException(String s){  
  super(s);  
 }  
} 

TestCustomException1.java TestCustomException1.java

class TestCustomException1{  

   static void validate(int age)throws InvalidAgeException{  
     if(age<18)  
      throw new InvalidAgeException("not valid");  
     else  
      System.out.println("welcome to vote");  
   }  

   public static void main(String args[]){  
      try{  
      validate(13);  
      }catch(Exception m){System.out.println("Exception occured: "+m);}  

      System.out.println("rest of the code...");  
  }  
} 

Output: 输出:

Output:Exception occured: InvalidAgeException:not valid
       rest of the code...

1) Yes: 1)是:

public class Entity
{
   public void foo() throws EntityException
   {
      ...
      throw new EntityException();
   }

   public static class EntityException extends Exception
   {
   ...
   }
}

2) You can wrap each custom exception in a container class like this: 2)您可以将每个自定义异常包装在容器类中,如下所示:

public class CustomExceptions
{
   public static class CustomExceptionA extends Exception
   {
   ...
   }

   public static class CustomExceptionB extends Exception
   {
   ...
   }
}

Here are examples relative to your question. 以下是与您的问题有关的示例。

An Exception , like a normal class, can be nested, inner, anonymous or top-level. 像普通的类一样, Exception可以嵌套,内部,匿名或顶级。

There's is a rule that does need to be followed though : checked exceptions are subclasses of Exception while non checked are subclasses of RunTimeExceptions (even though RTE are Exceptions, this is the way to go) 有一条规则确实需要遵循:已检查的异常是Exception子类,而未检查的是RunTimeExceptions子类(即使RTE是Exceptions,这也是解决方法)


Source 资源

public class MyException extends Exception{

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

}

class DifferentExceptions {

    class InnerException extends Exception {
        public InnerException(String msg) {
            super(msg);
        }
    }

    static class NestedException extends Exception {
        public NestedException(String msg) {
            super(msg);
        }
    }

    public static void main(String[] args) throws Exception {
        int example = 0;

        switch(example) {
        case 1 : throw new MyException("msg") ; 
        case 2 : throw new DifferentExceptions().new InnerException("msg") ; 
        case 3 : throw new DifferentExceptions.NestedException("msg") ; 
        default : throw new Exception("Not found") ;
        }
    }

}

1) A custom exception class can be defined either within the class it is intended for or in a separate class. 1)可以在要使用的异常类中定义自定义异常类,也可以在单独的类中定义它。

Example of the former - ThrowingClass.java : 前者的示例ThrowingClass.java

public class ThrowingClass {
    public static class ThrownInnerException extends Exception {
        public ThrownInnerException() {};
    }

    public void throwingMethod() throws ThrownInnerException {
        throw new ThrownInnerException();
    }
}

Example of the latter - ThrownException.java : 后者的示例ThrownException.java

public class ThrownException extends Exception {
    public ThrownException() {};
}

and ThrowingClass.java : ThrowingClass.java

public class ThrowingClass {
    public void throwingMethod() throws ThrownException {
        throw new ThrownException();
    }
}

2) Multiple custom exception classes can be defined within a single class file. 2)可以在一个类文件中定义多个自定义异常类。 Each custom exception class does not require its own class file. 每个自定义异常类不需要其自己的类文件。

Example - MultiThrowingClass.java : 示例MultiThrowingClass.java

public class MultiThrowingClass {
    public static class ThrownExceptionTypeOne extends Exception {
        public ThrownExceptionTypeOne() {};
    }

    public static class ThrownExceptionTypeTwo extends Exception {
        public ThrownExceptionTypeTwo() {};
    }

    public void throwingMethodOne() throws ThrownExceptionTypeOne {
        throw new ThrownExceptionTypeOne();
    }

    public void throwingMethodTwo() throws ThrownExceptionTypeTwo {
        throw new ThrownExceptionTypeTwo();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM