Jean-Francois Leveque

Nettoyage sur les conseils de SonarQube

Vulnerability : Define and throw a dedicated exception instead of using a generic one.

Using such generic exceptions as Error, RuntimeException, Throwable, and Exception prevents calling methods from handling true, system-generated exceptions differently than application-generated errors.

Noncompliant Code Example

public void foo(String bar) throws Throwable {  // Noncompliant
  throw new RuntimeException("My Message");     // Noncompliant
}

Compliant Solution

public void foo(String bar) {
  throw new MyOwnRuntimeException("My Message");
}
...@@ -27,12 +27,12 @@ public class BookServiceOldTest { ...@@ -27,12 +27,12 @@ public class BookServiceOldTest {
27 private BookServiceOld bookServiceOld; 27 private BookServiceOld bookServiceOld;
28 28
29 @Before 29 @Before
30 - public void setUp() throws Exception { 30 + public void setUp() {
31 bookServiceOld = new BookServiceOld(entityManager); 31 bookServiceOld = new BookServiceOld(entityManager);
32 } 32 }
33 33
34 @Test 34 @Test
35 - public void testAddBook() throws Exception { 35 + public void testAddBook() {
36 Book book = new Book(); 36 Book book = new Book();
37 bookServiceOld.addBook(book); 37 bookServiceOld.addBook(book);
38 Mockito.verify(entityManager).persist(book); 38 Mockito.verify(entityManager).persist(book);
...@@ -40,7 +40,7 @@ public class BookServiceOldTest { ...@@ -40,7 +40,7 @@ public class BookServiceOldTest {
40 40
41 41
42 @Test 42 @Test
43 - public void testAllBooks() throws Exception { 43 + public void testAllBooks() {
44 44
45 Book book = new Book(); 45 Book book = new Book();
46 book.setBookId(0); 46 book.setBookId(0);
......