I have a GWT project, in which I have a method like this:
#Override
public void onFailure(Throwable caught)
{
if (caught.getClass()== MyException.class)
{
//do specific stuff
}
else
{
// do generic stuf
}
}
Where MyException is a custom defined exception. A colleague of mine told me, that since this will get converted into JavaScript, where "there are no classes", this is not a very good idea and I should use:
#Override
public void onFailure(Throwable caught)
{
try {
throw caught;
} catch (MyException e) {
//do specific stuff
} catch (Throwable t) {
//do generic stuff
}
}
Since the first way actually works (at least on my side), will there be any problem if I continue to use it, or should I go with the second way?
You can use instanceof operator, it works -
#Override
public void onFailure(Throwable caught)
{
if (caught instanceof MyException)
{
//do specific stuff
}
else
{
// do generic stuf
}
}
I personnally prefer the second way.
Any Exception you do not catch, can be handle by a global UncaughtExceptionHandler.
GWT.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
public void onUncaughtException(Throwable e) {
// TODO Global Exception Handling ...
}
});
Related
I have a class that has many methods. All the methods throw one exception when data is not ready. In that case, I want to retry the method after a certain interval. So in catch, I need to add retry logic. The same logic i need to add for all methods.
Is there some way/pattern to execute same logic for all catch clause without copy paste
One way I could think of is to write my own Exception class and Throw that exception. And do this retry logic from My Exception class.
Is there any other better way to this?
class MyClass {
public void method1() {
try {
//do some logic
} catch (Exception e) {
//retry logic
//existing exception handling logic
}
}
public void method2() {
try {
//do some logic
} catch (Exception e) {
//retry logic
//existing exception handling logic
}
}
public void method3() {
try {
//do some logic
} catch (Exception e) {
//retry logic
//existing exception handling logic
}
}
}
EDIT:
class MyClass {
public void method1(int a, int b) {
try {
//do some logic
} catch (Exception e) {
Object args[] = {a,b};
executeLater("method1",args);
//retry logic
//existing exception handling logic
}
}
public void method2() {
try {
//do some logic
} catch (Exception e) {
Object args[] = null;
executeLater("method1",args);
//retry logic
//existing exception handling logic
}
}
public void method3(String abcd, int a) {
try {
//do some logic
} catch (Exception e) {
Object args[] = {abcd,a};
executeLater("method1",args);
//retry logic
//existing exception handling logic
}
}
public boolean executeLater(String methodName, Object args[]){
//Execute given method with the supplied args
return true;
}
}
Added code that shows what i would be doing in each catch clause
boolean processCompleted=false;
while(!processCompleted){
try{
doProcess();
processCompleted=true;
}catch(Exception e){
Thread.sleep(10000);
}
}
This might give you an idea. It keeps try to call doProcess until it doesn't throw exception. If any exception occurs, waits 10 seconds.
Well, you could extract the whole catch block content to a method and call that one, but this only works if your retry logic is not dependent on the specific method. And it also requires a try-catch in every method.
Instead, use functional programming to shorten it:
public class Playground
{
public static void main(String[] args)
{
new Playground().method2(1, 2);
new Playground().method1();
}
public void method1()
{
tryAndTryAgain(() -> {
// logic 1
System.out.println("no params");
throw new RuntimeException();
});
}
public void method2(int a, int b)
{
tryAndTryAgain(() -> {
// logic 2
System.out.println(a + " " + b);
throw new RuntimeException();
});
}
public static void tryAndTryAgain(Runnable tryThis)
{
try
{
tryThis.run();
}
catch (Exception e)
{
new Timer().schedule(new TimerTask()
{
#Override
public void run()
{
tryAndTryAgain(tryThis);
}
}, 1000);
// existing exception handling logic
}
}
}
The exact structure depends on your specfic implementation, but it should give you an idea how to structure it. The benefit is that all those methods can concentrate on the business logic, and the retry logic and exception handling are done in a util method. And that util method doesn't even need to know anything about parameters, methods, or anything, because all the business logic is contained in the Runnable.
I have a bunch of similar methods called from #Before beforeTest() in a test class:
//...
private void addClientDetails() {
try {
clientDetailsService.addClientDetails(testClient);
} catch (Exception e) {
}
}
private void addUserRoles() {
try {
adminController.addUserRoles(addedRoles);
} catch (Exception e) {
}
}
private void deleteAddedRoles() {
for (String role : addedRoles) {
try {
adminController.deleteUserRole(role);
} catch (Exception e) {
}
}
}
private void deleteClients() {
try {
clientsController.deleteClient(testClient.getClientId());
} catch (Exception e) {
}
}
//...
It is really unnecessary to catch possible exceptions and inconvenient to add some ifs here. These are the auxiliary methods to prepare tests or clean up after tests.
How to get rid of those ridiculous try {...} catch (...) {} constructs?
The idea was to create a new method with Runnable argument but this leads to even more cumbersome syntax:
private void deleteClients() {
trySilently(new Runnable() {
#Override
public void run() {
}
});
}
private void trySilently(Runnable task) {
try {
task.run();
} catch (Exception e) {
//do nothing
}
}
In JDK 1.8 method reference can help. But is there any beautiful solution in terms of JDK 1.7?
It is understood ignoring exceptions is a bad practice. Nevertheless the question is exactly how to do it in a graceful way.
You can declare that those methods throw exceptions, e.g.:
private void addClientDetails() throws Exception {
clientDetailsService.addClientDetails(testClient);
}
...then use reflection to call them:
String[] methods = {"addClientDetails", "addUserDetails" /*, ...*/};
for (String method : methods) {
try {
TestClass.class.getMethod(method).invoke(testObject);
}
catch (Exception e) {
// STRONGLY RECOMMEND DOING SOMETHING HERE SO YOU'RE NOT SILENTLY
// IGNORING EXCEPTIONS
}
}
(You'll need to keep the handler in deleteAddedRoles, though, since it loops, if you really want to ignore exceptions from adminController.deleteUserRole.)
Note: It seems very strange to be completely ignoring those exceptions. It's hard to imagine how you can trust your test results if you silently ignore exceptions from the test code. But I'm assuming you know what you're doing... :-)
In TestNG there is no problem for method annotated with #BeforeClass/#BeforeMethod to throw exception.
Why wouldn't you just
#BeforeClass
private void addClientDetails() throws Exception{
clientDetailsService.addClientDetails(testClient);
}
This also works for #Test methods.
Silenty catching exception is very bad idea. How could you trust your tests then? are you sure that exceptions tha occured is no problem indeed? If yes, then exception should not be thrown in first place.
Also, you could redesing your API to use unchecked exceptions. Just wrap any checked exceptions in RuntimeException, and throw RuntimeException.
Is there any helper method in the JDK or common libraries that does this:
if (resource instanceof AutoCloseable) {
((AutoCloseable) resource).close();
}
Just a one-liner to call an object's close() if applicable.
I know about try-with-resources, that's not applicable to this situation. And I know that not all classes that have a close() method implement AutoCloseable. But, I seem to write the above over and over..
There's something Apache Commons offers:
http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/IOUtils.html#closeQuietly(java.io.Closeable)
Here is apache commons closeQuietly adapted for AutoCloseable:
static void closeQuietly(AutoCloseable closeable) {
try {
if (closeable != null) {
closeable.close();
}
}
catch (Exception swallowed) {
}
}
since google sent me here for that case :)
Edit:
Check this:
class CloserHelper
{
public static void close(Object object)
{
if (object instanceof AutoCloseable)
{
try
{
((AutoCloseable) object).close();
}
catch (Exception ignored) { }
}
}
}
I can think to something like this
class CloserHelper
{
public static void close(AutoCloseable obj) throws Exception
{
obj.close();
}
}
Then
CloserHelper.close(resource);
If the object is not a AutoCloseable you cannot just call it
If you want to ignore exceptions
class CloserHelper
{
public static void close(AutoCloseable obj)
{
try
{
obj.close();
}
catch (Exception e) { }
}
}
I have this method on the server side talking communicating with the client side code through RPC.
#Override
public void registerStudent(param1, param2...) throws IllegalArgumentException {
//some code here
try {
//some code here
} catch (ConstraintErrorViolationException e) {
throw new RegisterFailedException();
}
}
I have this chunk of code handling failures.
#Override
public void onFailure(Throwable caught) {
displayErrorBox("Could not register user", caught.getMessage());
}
Currently, the onFailure() function does not differentiate between random exceptions and the specific exception I am looking to deal with and handle, which is RegisterFailedException.
How can I successfully handle the two different sort of errors properly?
So your exception
public class RegisterFailedException extends RuntimeException {
public RegisterFailedException () {
super();
}
}
and your method throws the exception like
throws new RegisterFailedException();
Then in onFailure() check
if (caught instanceof RegisterFailedException){
}
If RegisterFailedException is part of the client packages, you can simply use instanceof:
if(caught instanceof RegisterFailedException) {
// handle RegisterFailedException
}
else {
// handle other exceptions
}
May be it could be silly,but I want to clear my the technical understanding of this code:
import netscape.*;//ldap jar
public class A {
public void method() {
...
try {
//code is written here.
LDAPSearchResults lsr = ldi.search(LDAPConnectionInfo.MY_SEARCHBASE,LDAPConnectionInfo.MY_SCOPE,LDAPConnectionInfo.MY_FILTER,null,false);
while(lsr.hasMoreElements()){
LDAPEntry findEntry = (LDAPEntry)lsr.nextElement();
} catch(...) {
}
}
}
Now I call another class
public class B {
A a = new A();
//here I want to use attributeName
}
How could I access A class's member(in try block) in B class.
Any way to handle try block code for reuse in another class.
How could I handle all those exception in another class.
Any modification should I need...
Calling method of Object type.
public class C{
private String attributeName;
public String getAttributeName() {
return attributeName;
}
public Object method(){
attributeName=lAttribute.getName();
}
}
How could print this Object type method into String(in a jsp page)... any inputs
You'll need a member in class A and a getter:
public class A {
private String attributeName;
public String getAttributeName() {
return attributeName;
}
public void method(){
...
try {
//code is written here.
attributeName = lAttribute.getName();
}
catch() {
}
}
}
Then:
public class B {
A a = new A();
// somewhere
String str = a.getAttributeName();
}
There's no way to access a method's private variables like you did in the original example, as they only exist on the stack during the method call.
Edit: I noticed another question:
How could I handle all those exception in another class.
I assume you want to call your method somewhere else and catch the exceptions there. In that case you can use the throws keyword to communicate that your method will pass exceptions to the caller:
public class A {
public void method() throws IOException {
//code is written here.
String attributeName = lAttribute.getName();
}
public void anotherMethod() {
try {
method();
} catch(IOException ex) {
...
}
}
}
then if some other piece of code calls method it will be forced to either handle or further propagate the exception.
How could I handle all those exception in another class.
In your calling class you can catch Throwable (which will catch all exceptions and errors)
try {
....
}
catch (Throwable t) {
//do something with the throwable.
}
if you do not want to catch Errors (Ive only done this when messing around with ImageIO and had memory problems) in Java then catch Exception instead
Any way to handle try block code for reuse in another class
here you could create a method in another class and then call it within your try /catch block
public class XYX {
public void methodForTry() throws Exception {
//do something
}
}
try {
new XYZ().methodForTry();
}
catch (Exception e){
}
You may or may not want to create new XYZ here. It depends what state this object may or may not hold.
As to the last questions I think Tudor's answer covers this
Your question may be about extracting the code template
try { ... do stuff ... }
catch (MyFirstException e) { ...handle ... }
catch (MySecondException e) { ...handle ... }
... more catch ...
Where you only want to change the ... do stuff ... part. In that case you'd need closures, which are coming with Java 8, and today you'd need something quite cumbersome, of this sort:
public static void tryCatch(RunnableExc r) {
try { r.run(); }
catch (MyFirstException e) { ...handle ... }
catch (MySecondException e) { ...handle ... }
... more catch ...
}
where RunnableExc would be an
interface RunnableExc { void run() throws Exception; }
and you'd use it this way:
tryCatch(new RunnableExc() { public void run() throws Exception {
... do stuff ...
}});
why not return it?
public String method() {
String attributeName
try {
//code is written here.
attributeName = lAttribute.getName();
} catch(...) {
}
return attributeName;
}
public class B {
A a = new A();
String attributeName = a.method();
}