Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Throw Exception Error

Status
Not open for further replies.

simdan42

Programmer
Jul 17, 2002
118
US
I need help to determine the fix for this error:

C:\Documents and Settings\DHerron\Desktop\ProductApp.java:482: unreported exception java.io.IOException; must be caught or declared to be thrown
FileWriter exportFile = new FileWriter("c:\\DBFile.dat");
^
1 error

Tool completed with exit code 1

This is the code section:

public void DbtoText()
{
//Create text file objects.
FileWriter exportFile = new FileWriter("c:\\DBFile.dat");
PrintWriter exportFileObject = new PrintWriter(exportFile);

String ProductId_Hold;
String ProductDesc_Hold;
String QtyOnHand_Hold;
String QtyOrd_Hold;
String ExtPrice_Hold;
String UnitPrice_Hold;


ProductId_Hold = "";
ProductDesc_Hold= "";
QtyOnHand_Hold= "";
QtyOrd_Hold= "";
ExtPrice_Hold= "";
UnitPrice_Hold= "";


try
{
rsProduct = cmdProduct.executeQuery(
"Select * from Product;");
while (rsProduct.next())
{
ProductId_Hold = rsProduct.getString("ProductID");
ProductDesc_Hold = rsProduct.getString("ProductDesc");
QtyOnHand_Hold = rsProduct.getString("QtyOnHand");
QtyOrd_Hold = rsProduct.getString("QtyOrd");
UnitPrice_Hold = rsProduct.getString("UnitPrice");
ExtPrice_Hold = rsProduct.getString("ExtPrice");

exportFileObject.println("|" + ProductId_Hold + "|" + ProductDesc_Hold + "|" + QtyOnHand_Hold + "|" + QtyOrd_Hold + "|" + UnitPrice_Hold + "|" + ExtPrice_Hold );
}


//Close the file.
exportFileObject.close();

}

catch(SQLException error)
{
lblMessage.setText("Error during Edit. " + "Error: " + error.toString());
}
}
 
you have to catch the IOException thrown. The only exception that you don't have to catch is the RunTimeException.

Hence, wrap try-catch block around this:-

try {
FileWriter exportFile = new FileWriter("c:\\DBFile.dat");
//all your good stuff to follow

}
catch (IOException e){
//This is just an example
//the rule is when you catch something, do something
//useful, i.e. clean up codes etc
System.out.println("oppsss");
}


the other way to fix this is, create a function:-

public voind throwMe() throws IOException{

FileWriter exportFile = new FileWriter("c:\\DBFile.dat");
}

//here, it assumes the caller function catches the error

~za~
You can't bring back a dead thread!
 
C:\Documents and Settings\DHerron\Desktop\ProductApp.java:523: exception java.io.IOException is never thrown in body of corresponding try statement
catch (IOException e){
^
1 error

Tool completed with exit code 1


Code:

public void DbtoText() throws Exception
{
//Create text file objects.
FileWriter exportFile = new FileWriter("c:\\DBFile.dat");
PrintWriter exportFileObject = new PrintWriter(exportFile);

String ProductId_Hold;
String ProductDesc_Hold;
String QtyOnHand_Hold;
String QtyOrd_Hold;
String ExtPrice_Hold;
String UnitPrice_Hold;


ProductId_Hold = "";
ProductDesc_Hold= "";
QtyOnHand_Hold= "";
QtyOrd_Hold= "";
ExtPrice_Hold= "";
UnitPrice_Hold= "";


try
{
rsProduct = cmdProduct.executeQuery(
"Select * from Product;");
while (rsProduct.next())
{
ProductId_Hold = rsProduct.getString("ProductID");
ProductDesc_Hold = rsProduct.getString("ProductDesc");
QtyOnHand_Hold = rsProduct.getString("QtyOnHand");
QtyOrd_Hold = rsProduct.getString("QtyOrd");
UnitPrice_Hold = rsProduct.getString("UnitPrice");
ExtPrice_Hold = rsProduct.getString("ExtPrice");

exportFileObject.println("|" + ProductId_Hold + "|" + ProductDesc_Hold + "|" + QtyOnHand_Hold + "|" + QtyOrd_Hold + "|" + UnitPrice_Hold + "|" + ExtPrice_Hold );
}


//Close the file.
exportFileObject.close();

}

catch (IOException e){
lblMessage.setText("Error during Edit. " + "Error: " + e.toString());
}
}
 
your try statement is missing. check the example from my last response.

I know you have another tryh statement in there but that one is not for the "FileWriter" block



public void DbtoText() throws Exception
{
//Create text file objects.
FileWriter exportFile = new FileWriter("c:\\DBFile.dat");
PrintWriter exportFileObject = new PrintWriter(exportFile);

~za~
You can't bring back a dead thread!
 
Ok I fixed that but now I get this:

C:\Documents and Settings\DHerron\Desktop\ProductApp.java:261: unreported exception java.lang.Exception; must be caught or declared to be thrown
DbtoText();
^
1 error

Tool completed with exit code 1

When I call that procedure.

See COde:

public void actionPerformed(ActionEvent event)
{

//Test the command buttons
Object objSource = event.getSource();
if(objSource == btnAdd && event.getActionCommand () == "Add")
Add();
else if (objSource == btnAdd)
Save();
else if(objSource == btnEdit)
Edit();
else if(objSource == btnDelete)
Delete();
else if(objSource == btnExport)
DbtoText();
Cancel();
}
 
please read my first response carefully; it has all the answers you need. You need to catch any exceptions thrown by a method except runtimeException.

hence;

try{

DbtoText();
}
catch(Exception e){}



~za~
You can't bring back a dead thread!
 
DBtoTEXt() Method contains the try, catch block. It complies fine, however when I call the method it gives the error i mentioned above.
 
If I understood it correctly, you define DBtoText() method that "throws Exception".

public void DbtoText() throws Exception

The java compiler dictates that anybody that calls this method now has to catch the exception. The try-catch blocks you have inside the method do not have anything to do with this throws.

hence, anybody that calls this method has to catch it; or rethrow the exception.



~za~
You can't bring back a dead thread!
 
I think you misread my first response. Since you have handled the IOException using the try-catch block, you can take out that "throws Exception" from the DbToText() function. hence, take out throws Exception:-

public void DbtoText() {

In your other function,

else if(objSource == btnExport)
DbtoText();
Cancel();


will be compiled successfully without the try-catch block.


~za~
You can't bring back a dead thread!
 
OK yeah I got it to work. Thanks For your help! Please see my other thread on the FileReader you may be able to help me there too!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top