This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
Saying throws Exception is almost always poor design. The only circumstance where it might be all right is in something indefinite which might throw an Exception when overridden, and you don’t know the type, eg the Callable#call() method. You do not know whether implemented versions will throw an exception or not, so you have to declare the non‑specific Exception.
You should declare the exact type of Exception you expect. Possibly SQLException. But why are you throwing checked exceptions from a constructor in the first place? Does anything in that constructor declare a checked Exception? Can you catch it there?
The sort of Exception you should be throwing from a constructor is this sort or this, but they are unchecked and should be shown by @throws tags in the documentation comment.
I had to break the long lines in your code because they are so difficult to read, and had to edit it again because I didn’t quite do it right first time.
Joined: Jul 19, 2011
Ok im still playing around with java, useing book Java for dummies. I do know SQL. So i was trying to make an app to send data to SQL server. This can be done using this code.
If you look at the main part of the app
public static void main(String args) throws Exception
this app works, with no problems...
Now i want to take the same app but with out the main, to use as a class file with in my panel.class
im calling the panel.class ContactP.java
and im calling the SQL app ContactSql.java
so in the ContactP.java i write ContactSql csql = new ContactSql(Fn2, Ln2, Add2, Ct2, St2, Zp2, Hp2, Cp2, Em2);
and in the ContactSql.java i write public ContactSql(String Fn2, String Ln2, String Add2, String Ct2, String St2, String Zp2, String Hp2, String Cp2, String Em2)throws Exception
but i get an error when i compile the program.
error: unreported exception Exception; must be caught or declared to be thrown
ContactSql csql = new ContactSql(Fn2, Ln2, Add2, Ct2, St2, Zp2, Hp2, Cp2, Em2);