aspose file tools*
The moose likes JDBC and the fly likes SQLException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "SQLException" Watch "SQLException" New topic
Author

SQLException

Ravan Kumar
Greenhorn

Joined: Nov 15, 2012
Posts: 17
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

Joined: May 30, 2011
Posts: 44
Because, it is checked Exception.
Ravan Kumar
Greenhorn

Joined: Nov 15, 2012
Posts: 17
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

Joined: May 30, 2011
Posts: 44
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SQLException