Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SQLException

 
Ravan Kumar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why do i have to always declare SQLException when i write any jdbc code even when there is no need ?

without that codes doesn't compile, why ?
 
Ivan Franko
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because, it is checked Exception.
 
Ravan Kumar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i know this is checked exception but how to identify that there i have to declare it otherwise code wont compile ? i am just using methods i don't know their source code.
 
Ivan Franko
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but each method contains javadoc, and each javadoc section contains information about "throws Exceptions"

For example:


executeQuery

public ResultSet executeQuery(String sql)
throws SQLException
Executes the given SQL statement, which returns a single ResultSet object.
Parameters:
sql - an SQL statement to be sent to the database, typically a static SQL SELECT statement
Returns:
a ResultSet object that contains the data produced by the given query; never null
Throws:
SQLException - if a database access error occurs or the given SQL statement produces anything other than a single ResultSet object
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravan Kumar wrote:i know this is checked exception but how to identify that there i have to declare it otherwise code wont compile ? i am just using methods i don't know their source code.


The javadoc for that method tells you what checked exceptions it throws. The javadocs for each of those exceptions tell you whether they are checked or unchecked. Knowing the basic rules of checked and unchecked exceptions in Java tells you that you have to catch or declare the checked exceptions that method can throw. There's no need for its source code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic