{"id":20648934,"url":"https://github.com/codearte/catch-exception","last_synced_at":"2025-04-07T11:07:56.248Z","repository":{"id":22235848,"uuid":"25569063","full_name":"Codearte/catch-exception","owner":"Codearte","description":null,"archived":false,"fork":false,"pushed_at":"2022-08-13T20:45:35.000Z","size":1115,"stargazers_count":147,"open_issues_count":1,"forks_count":18,"subscribers_count":16,"default_branch":"master","last_synced_at":"2025-03-31T09:08:31.135Z","etag":null,"topics":["assertions","bdd","exception-handling","junit"],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Codearte.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-10-22T07:57:34.000Z","updated_at":"2024-06-27T13:55:35.000Z","dependencies_parsed_at":"2022-08-20T23:50:55.072Z","dependency_job_id":null,"html_url":"https://github.com/Codearte/catch-exception","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Codearte%2Fcatch-exception","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Codearte%2Fcatch-exception/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Codearte%2Fcatch-exception/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Codearte%2Fcatch-exception/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Codearte","download_url":"https://codeload.github.com/Codearte/catch-exception/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247640463,"owners_count":20971557,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["assertions","bdd","exception-handling","junit"],"created_at":"2024-11-16T17:11:46.726Z","updated_at":"2025-04-07T11:07:56.222Z","avatar_url":"https://github.com/Codearte.png","language":"Java","readme":"# catch-exception 2\n\nCatch and verify exceptions in a single line of code\n\n[![Build Status](https://travis-ci.org/Codearte/catch-exception.svg)](https://travis-ci.org/Codearte/catch-exception) [![Coverage Status](https://img.shields.io/coveralls/Codearte/catch-exception.svg)](https://coveralls.io/r/Codearte/catch-exception?branch=master) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/eu.codearte.catch-exception/catch-exception/badge.svg)](https://maven-badges.herokuapp.com/maven-central/eu.codearte.catch-exception/catch-exception) [![Apache 2](http://img.shields.io/badge/license-Apache%202-red.svg)](http://www.apache.org/licenses/LICENSE-2.0)\n\n\n*This is continuation of famous [catch-exception](https://code.google.com/p/catch-exception/) library created\nby Rod Woo*.\n\nThis library catches exceptions in a single line of code and makes them available for further analysis.\n\n# Usage\nThe most basic usage is:\n\n```java\nimport static com.googlecode.catchexception.CatchException.*;\n\n// given: an empty list\nList myList = new ArrayList();\n\n// when: we try to get the first element of the list\n// then: catch the exception if any is thrown\ncatchException(() -\u003e myList.get(1));\n\n// then: we expect an IndexOutOfBoundsException\nassert caughtException() instanceof IndexOutOfBoundsException;\n```\n\nThe last both lines of code can be combined in a single line of code if you like:\n\n```java\nverifyException(myList, IndexOutOfBoundsException.class).get(1);\n```\nMore information about the usage you find in the [Javadoc documentation](http://codearte.github.io/catch-exception/apidocs/overview-summary.html).\n\n# BDD-like\nIf you prefer a BDD-like approach, you can use [BDDCatchException](http://codearte.github.io/catch-exception/apidocs/com/googlecode/catchexception/apis/CatchExceptionBdd.html) for another code style:\n\n```java\nimport static com.googlecode.catchexception.apis.BDDCatchException.*;\n\n// given: an empty list\nList myList = new ArrayList();\n\n// when: we try to get the first element of the list\nwhen(() -\u003e myList.get(1));\n\n// then: we expect an IndexOutOfBoundsException\nthen(caughtException())\n        .isInstanceOf(IndexOutOfBoundsException.class)\n        .hasMessage(\"Index: 1, Size: 0\")\n        .hasNoCause();\n```\nThe assertions used here are part of [AssertJ](http://joel-costigliola.github.io/assertj/).\n\n# Hamcrest\nIf you prefer [Hamcrest](http://hamcrest.org/JavaHamcrest/) matchers to express assertions, you can use [CatchExceptionHamcrestMatchers](http://codearte.github.io/catch-exception/apidocs/com/googlecode/catchexception/apis/CatchExceptionHamcrestMatchers.html) with the following code style:\n\n```java\nimport static com.googlecode.catchexception.CatchException.*;\nimport static com.googlecode.catchexception.apis.CatchExceptionHamcrestMatchers.*;\n\n// given: an empty list\nList myList = new ArrayList();\n\n// when: we try to get the first element of the list\ncatchException(myList).get(1);\n\n// then: we expect an IndexOutOfBoundsException with message \"Index: 1, Size: 0\"\nassertThat(caughtException(),\n    allOf(\n        instanceOf(equalTo(IndexOutOfBoundsException.class)),\n        CatchExceptionHamcrestMatchers.hasMessage(\"Index: 1, Size: 0\"),\n        CatchExceptionHamcrestMatchers.hasNoCause()\n    )\n);\n```\n\n# Catch constructor exceptions\nCatch-exception supports exceptions that are thrown by constructors. \n\n```java\nverifyException(() -\u003e new Thing(\"baddata\"));\n```\n\nThanks to the community for the example.\n\n# Catch throwables\nIf you want to catch both throwables and exceptions have a look at the `catch-throwable` packages in [javadoc](http://codearte.github.io/catch-exception/apidocs/index.html?overview-summary.html). They provide the same API as the `catch-exception` packages but they belong to a different maven module.\n\n# JUnit4\nIf you want to handle expected exceptions, the [documentation](http://codearte.github.io/catch-exception/apidocs/com/googlecode/catchexception/CatchException.html) of catch-exception names some general reasons to prefer catch-exception in comparison to mechanisms that are provided by testing frameworks.\n\nBut some reasons that are specific to JUnit4 are outlined only here.\n\n## Collecting errors\nIf you want to combine the JUnit4's rules [ExpectedException](http://kentbeck.github.com/junit/javadoc/latest/org/junit/rules/ExpectedException.html) and [ErrorCollector](http://kentbeck.github.com/junit/javadoc/latest/org/junit/rules/ErrorCollector.html) you will find out: this won't work.\n\nCatch-exception instead can be easily combined with the error collecting rule:\n\n```java\n@Rule\npublic ErrorCollector collector = new ErrorCollector();\n\n@Test\npublic void testErrorCollectorWithExpectedException() {\n\n    // collect first error\n    collector.checkThat(\"a\", equalTo(\"b\"));\n\n    // collect second error\n    catchException(() -\u003e new ArrayList().get(1));\n    collector.checkThat(caughtException(), instanceOf(IllegalArgumentException.class));\n\n    // collect third error\n    collector.checkThat(1, equalTo(2));\n}\n```\n## Theories respectively parameterized tests\nSometimes you want to test for an [optional exception in a parameterized test](http://stackoverflow.com/questions/7275859/testing-for-optional-exception-in-parameterized-junit-4-test?rq=1). JUnit4's [ExpecteException](http://kentbeck.github.com/junit/javadoc/latest/org/junit/rules/ExpectedException.html) rule does not help in this case. This is another use case where catch-exception comes in quite handy.\n\n# Download\nGo to the [Installation page](https://github.com/Codearte/catch-exception/wiki/Installation) to get the latest release. This page provides also the Maven coordinates, prerequisites, and information about dependencies to other libraries.\n\n# Credits\nThanks to Rod Woo, the former author of catch-exception for creating this awesome library.\n\nThanks to Szczepan Faber who made some suggestions about a BDD-like syntax for catch-exception. Finally, his ideas triggered the enhancements that came with the 1.0.4 release.\n\n# Questions, Suggestions, Issues\nQuestions, suggestions and Issues are welcome and can be reported via [Issues page](https://github.com/Codearte/catch-exception/issues) of this project.\n\nPlease give me feedback of any kind. It is highly appreciated.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodearte%2Fcatch-exception","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodearte%2Fcatch-exception","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodearte%2Fcatch-exception/lists"}