DEV Community

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

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

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! βœ…

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

Top comments (0)