DEV Community

Cover image for Ignore.java: Skip any throwable code
Egit S.
Egit S.

Posted on • Originally published at esabook.wordpress.com on

1

Ignore.java: Skip any throwable code

Fuckit.py intro:

FuckIt.py uses state-of-the-art technology to make sure your Python code runs whether it has any right to or not. Some code has an error? Fuck it.


As frustrated man while writing to many SLOC for ignored exception at java like:

try{
    // run.throwable(); 
} catch (Exception ignored){ 
}
Enter fullscreen mode Exit fullscreen mode

meanwhile if should writing to many of should ignored code at runtime as like bellow, how to bypass exception?

int a = Integer.parse("none"); //should skip error, but runtime exception occurred at this line 
throw new JustSampleIgnoreException(); //should skip error, but not executed 
throw new OtherIgnoreException(); //should skip error, but not executed 
int b = 2/0; //should skip error, but not executed 
boolean c = run.call(); //should skip error, but not executed
Enter fullscreen mode Exit fullscreen mode

in traditional java may to use multiple try-catch, like:

try{
    int a = Integer.parse("none"); 
} catch (Exception ignored){ 
} 
try{
    throw new JustSampleIgnoreException(); 
} catch (Exception ignored){ 
} 
try{
    throw new OtherIgnoreException(); 
} catch (Exception ignored){ 
} 
try{
    int b = 2/0; 
} catch (Exception ignored){ 
} 
try{
    boolean c = run.call(); 
} catch (Exception ignored){ 
}
Enter fullscreen mode Exit fullscreen mode

but no. no. no. its not like as expected where with write long code is not cool.

I must simplifying code.

and ahaa, now we can write that to simplified code with small efforts to this:

// 0 as default value when error occurred int a = Ignore.get(()-> Integer.parse("none"), 0); 
Ignore.of(()-> throw new JustSampleIgnoreException(), ()-> throw new OtherIgnoreException()); 
int b = Ignore.get(()-> 2/0, 0); 
boolean c = Ignore.get(()-> run.call(), false); 
Enter fullscreen mode Exit fullscreen mode

how it work❓
try this snippet! ✅

/**
* Execute code with silent crash mode<br>
* usage:<br>
* <pre>
* Ignore.of(()-> {
* String bankName = o.infoPayment.namaBank.toUpperCase().replace("BANK", "");
* binding.tvBankName.setText(bankName);
* });
* </pre>
* <p>or</p>
* <pre>
* Ignore.of( ()-> Integer.parseInt(""),
* ()-> getClass().getSuperclass().cast(null),
* ()-> {\/*other operation*\/}
* );
*
*
* int o = Ignore.get(() -> 3232/0, 0);
* </pre>
*/
public final class Ignore {
private Ignore(Runner runner) {
try {
runner.run();
} catch (Exception ignore) {
if (BuildConfig.DEBUG)
Timber.w(ignore);
}
}
public static void of(Runner... runner) {
for (Runner r : runner) {
new Ignore(r);
}
}
public static <T> T get(final UncheckedFunction<T> function, T defaultValue) {
try {
return function.call();
} catch (Exception e) {
Timber.w(e);
return defaultValue;
}
}
public static <T> T getOrNull(final UncheckedFunction<T>... function) {
for (UncheckedFunction<T> f : function) {
T ret = getOrNull(f);
if (ret != null) return ret;
}
return null;
}
public static <T> T getOrNull(final UncheckedFunction<T> function) {
return get(function, null);
}
@FunctionalInterface
public interface Runner {
void run() throws Exception;
}
@FunctionalInterface
public interface UncheckedFunction<T> {
T call() throws Exception;
}
}
view raw Ignore.java hosted with ❤ by GitHub

image source: https://www.co2partners.com/every-rule-has-an-exception-especially-this-one/

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay