This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Cloud Application Architecture Patterns: Designing, Building, and Modernizing for the Cloud and have Kyle Brown, Bobby Woolf and Joseph Yodor on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

ERROR: Caught To Be Thrown?

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 158
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 158
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic