[英]How can I get the name of the class that throws a custom made Exception
I'm preparing a project for college where I need to write a custom made exception that will be thrown by a couple of classes in the same package when they were not initialized properly. 我正在准备一个用于大学的项目,我需要编写一个自定义的异常,当它们未正确初始化时,该异常将由同一包中的几个类抛出。 The problem is that I must let the user know which of those classes wasn't initialized properly (and throwed the exception)... So I was thinking about something like this:
问题是我必须让用户知道那些类中的哪些没有正确初始化(并引发了异常)……所以我在想这样的事情:
class InitializationException extends Exception {
private static final String DEFAULT_MSG =
"This " + CLASSNAME-THROWINGME + " had not been initialized properly!";
protected String msg;
InitializationException() {
this.msg = DEFAULT_MSG;
}
InitializationException(String msg) {
this.msg = msg;
}
}
(btw, can it be achieved via reflection?) (顺便说一句,可以通过反射实现吗?)
Look at Throwable.getStackTrace()
. 查看
Throwable.getStackTrace()
。 Each StackTraceElement
has getClassName()
. 每个
StackTraceElement
都有getClassName()
。 You can look at element [0]
to determine the origination of the exception. 您可以查看元素
[0]
以确定异常的来源。
Something like: 就像是:
StackTraceElement[] trace = theException.getStackTrace();
String className = trace[0].getClassName();
(Though I'm not quite sure whether you want the first element or the last in the trace.) (尽管我不太确定是要跟踪中的第一个元素还是最后一个。)
(And note that you can create a Throwable and do getStackTrace() on it, without ever throwing it, to find out who called you (which would be trace element 1).) (请注意,您可以创建一个Throwable并对其进行getStackTrace(),而无需抛出它,以找出谁打了您(这将是跟踪元素1)。)
I would just pass the throwing class into the constructor, like this: 我只是将throwing类传递给构造函数,如下所示:
public class InitializationException extends Exception {
public InitializationException(Class<?> throwingClass) { ... }
...
}
class InitializationException extends Exception {
private final String classname;
InitializationException(String msg, Object origin) {
super(msg);
this.classname = origin != null ? origin.getClass().toString() : null;
}
public String getClassname() {
return this.classname;
}
}
. 。 .
。 .
。 .
。 throw new InitializationException("Something went wrong", this);
抛出新的InitializationException(“出问题了”,这个);
Its a workaround: 解决方法:
you could pass the classname within the Constructor, something like 您可以在构造函数中传递类名,例如
throw new InitializationException(getClass().getName());
or may be call the class like 或者可以像
throw new InitializationException(this);
and handle the name extraction in your excpetin class 并在您的excpetin类别中处理名称提取
InitializationExcpetion(Object context){
this.name = getClass().getName()
}
The answer is you force the class throwing the exception to tell the exception which class it is: 答案是您强制类抛出异常以告知异常它是哪个类:
public class InitializationException extends Exception {
public InitializationException(Class<?> c) {
super( "The class " + c.getName()+ " had not been initialized properly!");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.