File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ERROR: Caught To Be Thrown?

 
Alan Jackson
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ive been getting an error saying that something needs to be caught to be thrown. What does this error mean?
 
Jimmy Die
Ranch Hand
Posts: 97
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


I'm not sure if I have enough information to answer, however your involved with throwing exceptions.

For example if you try to access a file, and that file is not available, java will give you an "Exception" which is like a error message. There is syntext involved with this, and most likely the syntext needs to be reviewed. It's hard to say without a little code to review, but you might be able to find out more. Look into try, catch statements and also throw and throws...
 
Hentay Duke
Ranch Hand
Posts: 198
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like your throwing an error but not catching it. Are you trying to throw the error or do you have a try block without a catch/finally block?
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please copy and paste the exact error message and the offending block of code (enough to give some context)? Paraphrasing typically loses critical information that we need to help you.

Thanks,

Layne
[ November 03, 2004: Message edited by: Layne Lund ]
 
Anand Karia
Ranch Hand
Posts: 156
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Alanistic

What i understand by your message is that probably you are making function and in which u r either calling object or make some kind a sql operation. For such purpose you have 2 option to deal with this situation.

Option 1
========
private void getValue() throws Exception
{
// blah blah blah
}

Option 2
private void getValue()
{

}
 
Anand Karia
Ranch Hand
Posts: 156
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Alanistic

What i understand by your message is that probably you are making function and in which u r either calling object or make some kind a sql operation. For such purpose you have 2 option to deal with this situation.

Option 1
========



Option 2
=========



Hope this would help.


ANAND KARIA
 
Alan Jackson
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies guys.

Anand, Ill give your options and try and get back to you.

In the mean time, here is the code thats giving me grief:

public void testListDirectory(){
try {
Object remoteObj = Naming.lookup("MyFileChecker");
FileChecker missCleo = (FileChecker)remoteObj;
String dirString = missCleo.listDirectory();
System.out.println(dirString);
}
catch(RemoteException rex){
fail(rex.toString());
}
}


The error I think is with missCleo...Im having software problems so I cant get the message up, but thats where the error is around abotu anyway.
[ November 04, 2004: Message edited by: Alan Jackson ]
 
Hentay Duke
Ranch Hand
Posts: 198
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without all your code it's hard to test, but perhaps your throwing some exception other than "RemoteException". Try adding a second catch block to catch "Exception", which is pretty generic but should catch anything that's thrown.
 
rahul V kumar
Ranch Hand
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You seem to be doing some lookup which might throw a NamingException so its better to have another catch block for a NamingException or a generic catch block.
 
Joel McNary
Bartender
Posts: 1824
Eclipse IDE Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that Hentay and Rahul are correct. Consider the following code:


This returns the following error:


Because no method in the try block is defined as throwing the exception.
Simply change line 12 to throw the SQLException:


and it compiles fine. Note that no SQLException is ever thrown; however, the method defines that it could be thrown, and so the compiler is happy (a subclass could override the method and throw the SQLException...)

Alternatively, you could just remove the try/catch block, since in your case you can't change the signatures of the methods that you are calling:


[ November 04, 2004: Message edited by: Joel McNary ]
 
Junilu Lacar
Bartender
Pie
Posts: 6563
22
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alan, as has been pointed out already, the error message means that some type of checked exception could be thrown by the code in the method. The solution is to either surround the code with a try-catch block that handles the checked exception or declare the exception in a throws clause.

The suggestions given so far have been to add an appropriate catch block to handle the checked exceptions. However, it looks like the code you posted is part of a JUnit TestCase. In this case, you should probably just declare your testListDirectory method to throw an Exception. The JUnit idiom for catch blocks is to add them if you are testing whether the class under test actually throws the exception. For example, assuming the class under test is the FileChecker class:



If you're only testing for the missCleo.listDirectory() functionality and not expecting any exceptions, then just let all Exceptions bubble up to the JUnit framework. In a JUnit test, a failure means the test ran successfully but didn't pass. An error, which is what any Exceptions you let bubble up to the framework will cause, means something else. An error means there is something wrong with your test setup/environment. An error doesn't mean your test failed, it means the test didn't run successfully and that the result of the test is inconclusive.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic