Eclipse (Java?) makes try/catch mandatory?
-
Hi, I'm usually programming in .net but now I have to do a project in java. Nothing wrong with it, but Eclipse demands try/catch blocks on sortof every call which might pose a problem, even if I have error handling in place. How do I get rid of that? I just need a quick check for something, but it even prevents me to compile it.
private void doSomething()
{
Properties properties = readProperties(chooser.getSelectedFile().getCanonicalPath()); <--- compile error
// do something with properties
}
private void readProperties(String location)
{
// do stuff including errorhandling
}While the location of my errorhandling might be debatable, this is for a proof of concept app where I *know* the input is correct.
A good programmer is someone who always looks both ways before crossing a one-way street. (Doug Linder)
-
Hi, I'm usually programming in .net but now I have to do a project in java. Nothing wrong with it, but Eclipse demands try/catch blocks on sortof every call which might pose a problem, even if I have error handling in place. How do I get rid of that? I just need a quick check for something, but it even prevents me to compile it.
private void doSomething()
{
Properties properties = readProperties(chooser.getSelectedFile().getCanonicalPath()); <--- compile error
// do something with properties
}
private void readProperties(String location)
{
// do stuff including errorhandling
}While the location of my errorhandling might be debatable, this is for a proof of concept app where I *know* the input is correct.
A good programmer is someone who always looks both ways before crossing a one-way street. (Doug Linder)
-
what's wrong? Exceptions need to be catched. Add the try/catch of the quick fix and go for it. If your code is fine it wont disturb you.
regards Torsten I never finish anyth...
-
While I do agree with you, I was wondering if I could "force" a compile and run it, as I know that there won't be an exception on that line.
A good programmer is someone who always looks both ways before crossing a one-way street. (Doug Linder)
-
Hi, I'm usually programming in .net but now I have to do a project in java. Nothing wrong with it, but Eclipse demands try/catch blocks on sortof every call which might pose a problem, even if I have error handling in place. How do I get rid of that? I just need a quick check for something, but it even prevents me to compile it.
private void doSomething()
{
Properties properties = readProperties(chooser.getSelectedFile().getCanonicalPath()); <--- compile error
// do something with properties
}
private void readProperties(String location)
{
// do stuff including errorhandling
}While the location of my errorhandling might be debatable, this is for a proof of concept app where I *know* the input is correct.
A good programmer is someone who always looks both ways before crossing a one-way street. (Doug Linder)
This is caused by the declaration of the method you are calling. It includes a throws part in the statement. If you don't wish to catch the exception, because you have error handling somewhere else in the application, then you can ignore the exception by adding a throws statement to your method.
private void doSomething() throws IOException
{
Properties properties = readProperties(chooser.getSelectedFile().getCanonicalPath());
}Please note this will only move the catch requirement up the call tree, meaning to whereever you are calling the doSomething method from. Which could be usefull when you have some type of generic exception handling in your code.
-
This is caused by the declaration of the method you are calling. It includes a throws part in the statement. If you don't wish to catch the exception, because you have error handling somewhere else in the application, then you can ignore the exception by adding a throws statement to your method.
private void doSomething() throws IOException
{
Properties properties = readProperties(chooser.getSelectedFile().getCanonicalPath());
}Please note this will only move the catch requirement up the call tree, meaning to whereever you are calling the doSomething method from. Which could be usefull when you have some type of generic exception handling in your code.
-
Thanks for your answer. This was what I've been looking for.
A good programmer is someone who always looks both ways before crossing a one-way street. (Doug Linder)
-
Hi, I'm usually programming in .net but now I have to do a project in java. Nothing wrong with it, but Eclipse demands try/catch blocks on sortof every call which might pose a problem, even if I have error handling in place. How do I get rid of that? I just need a quick check for something, but it even prevents me to compile it.
private void doSomething()
{
Properties properties = readProperties(chooser.getSelectedFile().getCanonicalPath()); <--- compile error
// do something with properties
}
private void readProperties(String location)
{
// do stuff including errorhandling
}While the location of my errorhandling might be debatable, this is for a proof of concept app where I *know* the input is correct.
A good programmer is someone who always looks both ways before crossing a one-way street. (Doug Linder)
Further to other answers, if a catch-able exception is thrown it must be caught in java. Their are two options, try-catch or use throws so that the calling method knows it could receive the exception and must act accordingly. If it is a runtime error, say out of memory, then it does not need to be handled as you cannot predict how it will occur and build time.
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
-
...so you don't have code assist? this is also provided as quick tip.
regards Torsten I never finish anyth...
I do have code assist, but for some reason all I got was a compile error, not a hint on how to disable "possible exceptions". I must admit I'm not using the standard Eclipse, but an Eclipse version which is tailored for the Sonic ESB, so maybe there's something in there which prevents it. And the tips are appreciated, as always.
A good programmer is someone who always looks both ways before crossing a one-way street. (Doug Linder)