https://github.com/firaja/capture4j
This library aims to remove from your code bulky try-catch blocks and make your code easier to read and maintain.
https://github.com/firaja/capture4j
bettercode clean-code exceptions java-library maintainability
Last synced: 27 days ago
JSON representation
This library aims to remove from your code bulky try-catch blocks and make your code easier to read and maintain.
- Host: GitHub
- URL: https://github.com/firaja/capture4j
- Owner: firaja
- License: mit
- Created: 2019-12-12T10:49:30.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-10-20T10:02:06.000Z (over 5 years ago)
- Last Synced: 2025-01-19T10:09:59.103Z (about 1 year ago)
- Topics: bettercode, clean-code, exceptions, java-library, maintainability
- Language: Java
- Homepage:
- Size: 70.3 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# Capture4J
[](https://travis-ci.org/firaja/capture4j)
[](https://codeclimate.com/github/firaja/capture4j/maintainability)
[](https://codecov.io/gh/firaja/capture4j)

This library aims to remove from your code bulky try-catch/if-else blocks and make your code easier to read and maintain.
Write less repetitive checks and enjoy coding efficiently.
## Getting Started
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.
### Prerequisites
Make sure you have installed all of the following prerequisites on your development machine:
* **Java JDK 1.8+** - You can use a JDK from any vendor, but it is recommended to use
[Oracle JDK](https://www.oracle.com/technetwork/java/javase/downloads/index.html) or
[OpenJDK](https://openjdk.java.net/install/)
* **Maven 3+** - [Download and install](https://maven.apache.org/install.html)
### Installing
#### Git
```shell script
git clone git@github.com:firaja/capture4j.git
cd capture4j
mvn clean install
```
#### Maven (requires GitHub Authentication)
Insert the following dependency in your `pom.xml`
```xml
dev.firaja.utils
capture4j
0.1.1
```
Then run
```shell script
mvn install
```
## Profit
Here a classic example where a method returns the same value in case of *error*
```java
public int doSomething(MyObject myObject)
{
if(myObject == null || myObject.getId() == null)
{
return -1;
}
String name;
try
{
name = readNameFromSomewhere(myObject.getId());
}
catch (EntryNotFoundException e)
{
return -1;
}
if(name != null && name.indexOf(45) == 'a')
{
return -1;
}
return 1;
}
```
while the same method can be rewritten with **capture4j** like this
```java
@EasyCapture(returns = "-1")
public int doSomething(MyObject myObject)
{
String name = readNameFromMyDB(myObject.getId());
return name.indexOf(45) == 'a' ? 1 : -1;
}
```
You can specify the type of exception you want to treat
```java
@EasyCapture(what = IllegalStateException.class, returns = "illegal")
@EasyCapture(what = NullPointerException.class, returns = "null pointer :(")
public String doSomething()
{
...
}
```
Or even use a custom handler for standard or custom exceptions
```java
@Capture(what = MyException.class, with = MyHandler.class)
public long doSomething()
{
...
}
```
```java
class MyHandler implements Handler
{
public Long handle(Throwable theException) // <- MyException
{
long fallback = doSomeCalculation();
logger.warn("Something went wrong :( but I'returning {}.", fallback, theException);
return fallback;
}
}
```
## Versioning
 
We use [SemVer](http://semver.org/) for versioning.
For the versions available, see the [tags on this repository](https://github.com/firaja/capture4j/tags).
## Contributing
Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests to us.
## Authors
* **David Bertoldi** - *Initial work* - [firaja](https://github.com/firaja)
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details