[英]Multiple catch statements with the same exception types
我一直在寻找一个答案,但是还没有找到答案。
基本上,我试图通过GUI连接到数据库服务器。 我的老板希望能够输入所有字段,然后检查它们是否有效,然后,如果有任何无效条目,他希望我将文本变成红色,表示该字段无效。 我有try语句捕获ClassNotFoundException和SQLException。 因为有多个字段需要检查,所以我尝试使用一组if语句来检查连接信息。 这是下面的代码,我希望这是有道理的...
//The cancel boolean values in this code are used elsewhere to regulate the Threads
try
{
//attempt connection here
}
catch(ClassNotFoundException | SQLException e)
{
String[] errors = new String[4]; //This will create a String array of the errors it catches
//and will later get called into a method that displays
//the messages in a JOptionPane.showMessageDialog()
if (e.getMessage().startsWith("The TCP/IP connection to the host"))
{
errors[0] = "SQL CONNECTION FAILED: Please check the server URL you entered to make sure it is correct.";
cancel = true;
mGUI.serverNameTextField.setForeground(Color.RED);
}
if (e.getMessage().startsWith("Login failed for user"))
{
errors[1] = "LOGIN FAILED: You do not have sufficient access to the server.";
cancel = true;
}
if (e.getMessage().startsWith("Cannot open database"))
{
errors[2] = "SQL CONNECTION FAILED: Please check the database name you entered to make sure it is correct.";
cancel = true;
mGUI.dbNameTextField.setForeground(Color.RED);
}
mGUI.reportErrors(errors); //Method where it reports the String[] array of errors
//However, the 'errors' parameter only returns one error
//message at a time, which is the problem.
谢谢你的帮助!
**** 编辑 * ** * **我找到了解决方案,所以希望这会对某人有所帮助。 我更改了if语句,以添加用于检查特定错误代码的AND参数。 您可以通过设置断点并查看调试透视图来找到错误代码,或者可以执行我的操作并设置打印语句以查看错误代码。 这是打印语句:
System.out.println(((SQLException) e).getErrorCode());
这是我的新声明:
try
{
//attempt connection here
}
catch(SQLException | ClassNotFoundException e)
{
if (e instanceof SQLServerException && ((SQLServerException) e).getErrorCode() == 0)
{
//code here
}
else{
//code here
}
System.out.println(((SQLException) e).getErrorCode()); //Here is the print statement to see the error code.
if (e instanceof SQLServerException && ((SQLServerException) e).getErrorCode() == 4060)
{
//code here
}else{
//code here
}
if(cancel != true)
{
//code here
}
}
您可以通过多种方式进行
1个具有多个具有共同功能的渔获物
}catch (ClassNotFoundException e){
handleError(e);
}catch (SQLException e){
handleError(e);
}
其中handleError将异常作为参数。
您似乎没有做任何其他事情,因此您可以将它们都合并为一个例外
}catch(Exception e){
}
它将捕获所有内容,但是您对错误处理的控制要少得多。
例外的一般原则是,在最有可能处理它们的时候对它们进行处理。
您似乎有非常不同的异常,并且大概是代码中抛出的TCP异常与连接数据库时抛出的SQLException不同(我在这里可能是错的,因为我不知道其余的代码是什么样子)。 因此不会有一组异常处理程序,每种类型都有一个异常处理程序更有意义。 同样要让布莱恩·罗奇(Bryan Roach)重申,消除文本歧义不是一个好主意。
try {
...
} catch (java.net.SocketException e) {
e[0] = "tcp error";
} catch (java.sql.SQLException e) {
e[1] = "sql exception happened";
}
而且您的字符串数组似乎有点风险,可能是
ArrayList errors = new ArrayList();
errors.add("Some tcp error");
errors.add("Some db error");
然后为您报告错误
mGUI.reportErrors(errors.toArray())
将保留您的接口,而不会浪费您必须为数组分配额外的元素并具有空条目。 我不确切知道您的问题是什么,但是您暗示GUI没有显示多个错误。 可能有一个检查在数组的第一个空元素处停止。 假设e [2]和e [4]已填充,则由于e [3]为空,因此在遍历错误时可能会停止。 我再次假设,因为我不知道该代码是什么样
从上面的注释中看来,您要针对在单个catch块中捕获的各种Exception类型具有不同的逻辑。 如果是这样,您可以执行以下操作:
...
catch(ClassNotFoundException | SQLException e) {
String[] errors = new String[4];
if (e instanceof ClassNotFoundException) {
//do something here
}
if (e instanceof SQLException) {
//do something else here
}
...etc
}
这应该可以工作,但是使用多个catch块可能就像其他人建议的一样容易:
}catch (ClassNotFoundException e){
handleError(e);
}catch (SQLException e){
handleError(e);
}
我的意思不是冒犯,但是代码处理异常的方式可能会引起一些麻烦。 例如:
if (e.getMessage().startsWith("Cannot open database")) {
这里的代码依赖于支持库,该库抛出异常以使用相同的文本描述,但是如果您切换到另一个JVM版本,使用其他数据库驱动程序等,则此描述可能会更改。按异常类型进行查找可能更安全,而不是异常描述。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.