如何处理“默认构造函数无法处理异常类型ClassNotFoundException”错误

huangapple go评论75阅读模式
英文:

How to handle "Default constructor cannot handle exception type ClassNotFoundException" error

问题

错误信息是“默认构造函数无法处理由隐式超级构造函数抛出的ClassNotFoundException异常类型。必须定义显式构造函数”,位于此类中的“= new DriveStore()”声明中。

import ..... ;

@WebServlet("/create_drive")
public class create_Drice extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private DriveStore drive_store = new DriveStore();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ...
        ...
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		...
        ...
	}
}

在doGet()和doPost()之外的try-catch块不起作用,而且drive_store在两者中都被使用。如何解决这个问题?

英文:

The error is "Default constructor cannot handle exception type ClassNotFoundException thrown by implicit super constructor. Must define an explicit constructor"
in the " = new DriveStore()" declaration in this class

import ..... ;

@WebServlet("/create_drive")
public class create_Drice extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private DriveStore drive_store = new DriveStore();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ...
        ...
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		...
        ...
	}
}

try-catch block outside doGet() and doPost did not work and drive_store is used in both.
How could this be solved?

答案1

得分: 3

编译错误的原因是使用默认构造函数创建 DriveStore 实例可能会抛出 ClassNotFoundException,但编译器既不能自动处理异常,也不能重新抛出异常,因为所示的 create_Drice 类的隐式默认构造函数没有声明任何异常(在Java中,隐式构造函数永远不会抛出已声明的异常)。

正如错误消息所示,解决方法是向 create_Drice 类添加显式构造函数。此构造函数可以像这样声明并抛出异常:

public class create_Drice {
	
	private DriveStore drive_store;
	
	public create_Drice() throws ClassNotFoundException {
		drive_store = new DriveStore();
	}

}

... 或者可以以某种方式处理异常:

public class create_Drice {
	
	private DriveStore drive_store;
	
	public create_Drice() {
		try {
			drive_store = new DriveStore();
		} catch (ClassNotFoundException exc) {
			throw new RuntimeException("无法创建 DriveStore", exc);
		}
	}

}

这里所示的异常“处理”是将其转换为运行时异常,不需要在Java中声明。当然,您也可以实现其他方式来处理异常。

英文:

The reason for the compile error is that the creation of a DriveStore instance using the default constructor might throw a ClassNotFoundException, but the Compiler can neither automatically handle the exception nor rethrow it, because the implicit default constructor of the shown class create_Drice does not declare any exceptions (implicit constructors can never throw declared exceptions in Java).

As the error message says, the fix is to add an explicit constructor to the create_Drice class. This constructor can either declare and throw the exception like this:

public class create_Drice {
	
	private DriveStore drive_store;
	
	public create_Drice() throws ClassNotFoundException {
		drive_store = new DriveStore();
	}

}

... or it can handle the exception in some way:

public class create_Drice {
	
	private DriveStore drive_store;
	
	public create_Drice() {
		try {
			drive_store = new DriveStore();
		} catch (ClassNotFoundException exc) {
			throw new RuntimeException("Cannot create DriveStore", exc);
		}
	}

}

The "handling" of the exception shown here is the conversion to a runtime exception, which does not have to be declared in Java. Of course you can implement some other way to handle the exception.

huangapple
  • 本文由 发表于 2023年1月9日 06:14:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051639.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定