Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore Exceptions in Java

I have the following code:

TestClass test=new TestClass(); test.setSomething1(0);  //could, but probably won't throw Exception test.setSomething2(0);  //could, but probably won't throw Exception 

I would like to execute: test.setSomething2(0); even if test.setSomething(0) (the line above it) throws an exception. Is there a way to do this OTHER than:

try{    test.setSomething1(0); }catch(Exception e){    //ignore } try{    test.setSomething2(0); }catch(Exception e){    //ignore } 

I have a lot of test.setSomething's in a row and all of them could throw Exceptions. If they do, I just want to skip that line and move to the next one.

For clarification, I don't care if it throws an Exception, and I can't edit the source code of the code which throws this exception.

THIS IS A CASE WHERE I DON'T CARE ABOUT THE EXCEPTIONS (please don't use universally quantified statements like "you should never ignore Exceptions"). I am setting the values of some Object. When I present the values to a user, I do null checks anyway, so it doesn't actually matter if any of the lines of code execute.

like image 900
Nick Avatar asked Feb 22 '15 15:02

Nick


People also ask

Can I ignore an exception Java?

To ignore an exception in Java, you need to add the try... catch block to the code that can throw an exception, but you don't need to write anything inside the catch block.

How do I avoid exceptions?

Another way to avoid exceptions is to return null (or default) for extremely common error cases instead of throwing an exception. An extremely common error case can be considered normal flow of control. By returning null (or default) in these cases, you minimize the performance impact to an app.

How do you stop an exception in Java?

The try-catch is the simplest method of handling exceptions. Put the code you want to run in the try block, and any Java exceptions that the code throws are caught by one or more catch blocks. This method will catch any type of Java exceptions that get thrown. This is the simplest mechanism for handling exceptions.

How do I ignore NullPointerException?

How to avoid the NullPointerException? To avoid the NullPointerException, we must ensure that all the objects are initialized properly, before you use them. When we declare a reference variable, we must verify that object is not null, before we request a method or a field from the objects.


1 Answers

There is no way to fundamentally ignore a thrown exception. The best that you can do is minimize the boilerplate you need to wrap the exception-throwing code in.

If you are on Java 8, you can use this:

public static void ignoringExc(RunnableExc r) {   try { r.run(); } catch (Exception e) { } }  @FunctionalInterface public interface RunnableExc { void run() throws Exception; } 

Then, and implying static imports, your code becomes

ignoringExc(() -> test.setSomething1(0)); ignoringExc(() -> test.setSomething2(0)); 
like image 147
Marko Topolnik Avatar answered Sep 30 '22 11:09

Marko Topolnik