Two Laptop Bag*
The moose likes Beginning Java and the fly likes ERROR: Caught To Be Thrown? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ERROR: Caught To Be Thrown?" Watch "ERROR: Caught To Be Thrown?" New topic
Author

ERROR: Caught To Be Thrown?

Alan Jackson
Greenhorn

Joined: Oct 26, 2004
Posts: 26
Ive been getting an error saying that something needs to be caught to be thrown. What does this error mean?
Jimmy Die
Ranch Hand

Joined: Nov 20, 2003
Posts: 97
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...


Jimmy Die
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
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

Joined: Dec 06, 2001
Posts: 3061
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 ]

Java API Documentation
The Java Tutorial
Anand Karia
Ranch Hand

Joined: Sep 25, 2004
Posts: 156

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()
{

}


Love is GOD and GOD is Love.
Anand Karia
IT Concretor.......
M/s. Anand Karia Concreting IT
Anand Karia
Ranch Hand

Joined: Sep 25, 2004
Posts: 156

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

Joined: Oct 26, 2004
Posts: 26
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

Joined: Oct 27, 2004
Posts: 198
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

Joined: May 20, 2003
Posts: 82
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

Joined: Aug 20, 2001
Posts: 1817

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 ]

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4474
    
    6

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.


Junilu - [How to Ask Questions] [How to Answer Questions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ERROR: Caught To Be Thrown?