[英]Creating a custom SQL checked exception in java?
I know how to create a basic custom checked exception, but I want to be able to create an exception that checks the following: 我知道如何创建一个基本的自定义检查异常,但是我希望能够创建一个检查以下各项的异常:
So I have the following : 所以我有以下内容:
package asl.exceptions;
public class QueueDoesNotExistException extends Exception {
public QueueDoesNotExistException(){
}
}
I am not sure how to handle this, I dint find any resources for this, or am I looking at the wrong place? 我不确定如何处理此问题,我没有找到任何相关资源,还是我看错地方了?
I read this : https://docs.oracle.com/javase/tutorial/jdbc/basics/sqlexception.html 我读了这个: https : //docs.oracle.com/javase/tutorial/jdbc/basics/sqlexception.html
But dint find it useful. 但是,发现它很有用。
you need to catch DB-specific exception thrown by your JDBC driver first. 您需要首先捕获JDBC驱动程序引发的特定于数据库的异常。 then you can wrap it in your exception and propagate further. 那么您可以将其包装在异常中并进一步传播。 but exception-driven logic is rarely a good design. 但是异常驱动逻辑很少是一个好的设计。
You should never let the flow get to the point where a customer is trying to access a message from a non-existent queue. 您永远不应让流程到达客户试图从不存在的队列访问消息的地步。 You check for queue existence first thing in the workflow and abort the workflow with an exception, without ever making the second query for messages in the queue. 您首先检查工作流中是否存在队列,然后异常中止工作流,而无需对队列中的消息进行第二次查询。
Based on comments, it seems you are trying to validate the query itself , as opposed to ensuring that it cannot return invalid data. 基于注释,似乎您正在尝试验证查询本身 ,而不是确保查询不能返回无效数据。 Apparently, you also want to distinguish between a query that returns zero rows on account of the specified queue not being recorded in the DB, and a query that returns zero rows because there are simply no messages available in that queue. 显然,您还希望区分由于未在数据库中记录指定队列而返回零行的查询与返回零行的查询,因为该队列中根本没有消息可用。 I'm doubtful of the usefulness of these pursuits, but if you insist, then here's an approach you could take: 我怀疑这些追求是否有用,但是如果您坚持要这样做,那么可以采取以下方法:
Connection
is not in auto-commit mode ( connection.setAutoCommit(false)
) 确保您的Connection
未处于自动提交模式( connection.setAutoCommit(false)
) queue_table
by its ID. 尝试从queue_table
通过其ID选择指定的队列。 If that fails, then roll back the connection's transaction and throw the exception of your choice. 如果失败,则回滚连接的事务并抛出您选择的异常。 I apologize if that seems simple or nonmagical, or whatever. 如果这看起来简单或不可思议,则我深表歉意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.