Skip to content

Throw any Java Exception anywhere without the need of catching them nor wrapping them into RuntimeException

License

Notifications You must be signed in to change notification settings

qoomon/unchecked-exceptions-java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

90 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Unchecked Exception

Build

Known Vulnerabilities

Maintainability

Test Coverage

Maven Central

This lib hase zero dependencies and can be used to throw checked exceptions without declaring this in your method's head throws clause. And without wrapping Exception into a RuntimeException. Basically you can throw any Exception everywhere without catching them, happy throwing :-D

Methods

unchecked(Exception) rethrows any exception as unchecked exception, without wrapping exception.

  throw unchecked(checkedException);

unchecked(LambdaFunction) returns result or rethrows exception from lambda function as unchecked exception, without wrapping exception.

  return unchecked(() -> methodThrowingCheckedException())

Usage Examples

Try-Catch Block

Regular Code

  import static me.qoomon.UncheckedExceptions.*;

  public class Example {
      
      void example() {
          URL url;
          // code polition with try catch block
          try {
            url = new URL("https:/www.example.org");
          } catch (MalformedURLException e) {
            // ugly exception wrapping
            throw RuntimeException(e); 
          }
          System.out(url);
      }
  }

Unchecked Exception Code

  import static me.qoomon.UncheckedExceptions.*;

  public class Example {
      
      void example() {
        // get rid of code polition with try catch block and exception wrapping
        URL url = unchecked(() -> new URL("https:/www.example.org"));
        System.out(url);
      }
  }

Stream

Regular Code

  
    import static me.qoomon.UncheckedExceptions.*;
  
    public class Example {
        
        void example() {
          Stream.of("https:/www.example.org")
            .map(url -> {
              // code polition with try catch block
              try {
                return new URL(url);
              } catch (MalformedURLException e) {
                // ugly exception wrapping
                throw new RuntimeException(e);
              }
            });
        }
    }

Unchecked Exception Code

  
    import static me.qoomon.UncheckedExceptions.*;
  
    public class Example {
        
        void example() {
            Stream.of("https:/www.example.org")
                // get rid of code polition with try catch block and exception wrapping
                .map(url -> unchecked(() -> new URL(url)));
            }
    }

About

Throw any Java Exception anywhere without the need of catching them nor wrapping them into RuntimeException

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Contributors 3

  •  
  •  
  •  

Languages