File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes What's wrong with this code???? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "What Watch "What New topic
Author

What's wrong with this code????

hmehta
Greenhorn

Joined: Aug 30, 2000
Posts: 20
I get the following error message on compiling this code. Whats wrong with this I cannot figure out!!
Exception java.sql.SQLException is never thrown in the body of the corresponding try statement.


(edited by Cindy to format code - also hmehta,

Please change your name to be compliant with JavaRanch's naming policy.
Your displayed name should be 2 separate names with more than 1 letter each. We really would prefer that you use your REAL name.
You can change your name: here.
Thanks,
Cindy)

[ September 03, 2002: Message edited by: Cindy Glass ]
deekasha gunwant
Ranch Hand

Joined: May 06, 2000
Posts: 396
Hi hmehta,
This error comes when u r unnecessarily catching an exception. so it seems that throughout ur try block u r not using any function/statement that throws SQLException and still u r catching it. that's why compiler is complaining.
juct remove the try catch block and see if the program compiles.
regards
deekasha
prakash muthu
Ranch Hand

Joined: Sep 06, 2000
Posts: 75
hi
you got the answer or not. what i'm thinking is, i can add any number of exception in such a order. Right!. But one guy is replied like, remove the exception, it will work.
let me know the status.
with regards
Prakash.M
(prakashem@rediffmail.com)


Prakash.M<BR>Bangalore- India<BR>(prakashem@rediffmail.com)
lakshmanan eswaramoorthy
Greenhorn

Joined: Aug 30, 2002
Posts: 2
hi,
In my code ,i have called one method which might throw the exception. But in that method definition i just say throws Exception.
But the method in which i called this method should handle the SQLException seperatly.
Please help me regarding this.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
You are not allowed to catch a checked exception (such as SQLException) that can never be thrown.
Which method are you calling that should be throwing SQLException ?


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

hmehta,
Please use the
UBB code tags when posting code. It is hard to read your post.
Besides your stated problem, heads up for this one:
if(status == "Available")
status is a String so you must compare the values of the String objects like this:
if( status.equals( "Available" ) )
or if( "Available".equals( status ) )
otherwise, you are comparing the references rather than the value of Strings.
Jamie
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

Another heads up for this potential problem:
...
+ " a.bkg_purpose = '" + bookingtext + "'"
+ status_sql;
where status_sql = "and a.bkg_confirmed = 'Y'" (or some other value)

When you go to execute your SQL, you are missing a space between the value of status_sql and the line before it. It will translate to something like " a.bkg_purpose = 'bookingtext_value'and a.bkg_confirmed = 'Y'"
Jamie
lakshmanan eswaramoorthy
Greenhorn

Joined: Aug 30, 2002
Posts: 2
hi,
I didn't throw the exception in called method to the calling method . After throwing the exception,the code compiles correctly.
The wrong code
import java.sql.*;
class UseDBConn
{
public static void main(String[] args)
{
try
{
DBConn db = new DBConn;
String name=dbl.getName();
}
catch (SQLException e)
{
System.out.println("SQL Exception handled ");
}
catch (Exception e)
{
System.out.println("Exception throws");
}


}
}
public class DBConn
{
public String getName() {
try
{
//Some SQL Statements
}
catch (Exception e)
{
e.printStackTrace();

}finally{ con.close(); }
return //A string variable
}
}
Corrected Code
import java.sql.*;
class UseDBConn
{
public static void main(String[] args)
{
try
{
DBConn db = new DBConn;
String name=dbl.getName();
}
catch (SQLException e)
{
System.out.println("SQL Exception handled ");
}
catch (Exception e)
{
System.out.println("Exception throws");
}


}
}
public class DBConn
{
public String getName() throws Exception {
try
{
//Some SQL Statements
}
catch (Exception e)
{
e.printStackTrace();
throw e;
}finally{ con.close(); }
return //A string variable
}
}
Here I have one doubt.
Do i need to rethrow the exception in catch block?
Because i have given throws Exception in method definition
Thank you
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

"Here I have one doubt.
Do i need to rethrow the exception in catch block?
Because i have given throws Exception in method definition"
This is more of a design question. Do I want to deal with the exception in the method that it occurs, then use try/catch block. If you want to deal with the exception in the calling method, then you would not have any try/catch blocks in your method, but the method would have a throws clause. I guess that you could have a try/catch block, then throw another exception in the catch block ( deal with the problem, then pass it up as well ), but why would you need to deal with the same problem twice?
[ August 30, 2002: Message edited by: Jamie Robertson ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What's wrong with this code????