在遇到我确实需要捕捉异常的地方之前,一切都很棒。当我放置
jdbcTemplate.query(something...)
in
try{}
阻止我得到:
Unreachable catch block for SQLException. This exception is never thrown from the try statement body.
在这种情况下我该怎么办?
try{
personIdReturnedByDb = jdbcTemplate.queryForInt(sql, p.getEmail(),
p.getName(), p.getSurname(), encPw, dateSql);
}
catch(SQLException sa){
}
谢谢,
答案 0 :(得分:33)
这是因为任何SQLException
方法javadoc link)都没有抛出JdbcTemplate.query(...)
,这是一个已检查的异常.Spring会将此转换为DataAccessException之一,是更通用的运行时异常系列,以抽象出任何特定的底层数据库实现。
答案 1 :(得分:5)
您应该捕获JdbcTemplate异常
即
try
{
// Your Code
}
catch (InvalidResultSetAccessException e)
{
throw new RuntimeException(e);
}
catch (DataAccessException e)
{
throw new RuntimeException(e);
}
答案 2 :(得分:0)
InvalidResultSetAccessException是DataAccessException,因此在您的情况下无需捕获它。 并且DataAccessException已经是RuntimeException,因此无需抛出Runtime异常。 但是您应该抛出应用程序的特定异常,例如:
# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
# Before you set out to tweak and tune the configuration, make sure you
# understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
#cluster.name: my-application
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
#node.name: node-1
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
#path.data: /path/to/data
#
# Path to log files:
#
#path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
#network.host: 192.168.0.1
#
# Set a custom port for HTTP:
#
#http.port: 9200
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
#discovery.zen.ping.unicast.hosts: ["host1", "host2"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
#
#discovery.zen.minimum_master_nodes:
#
# For more information, consult the zen discovery module documentation.
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
#gateway.recover_after_nodes: 3
#
# For more information, consult the gateway module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true