Skip to content

Commit a0b0e1c

Browse files
committed
Java Custom Exception
1 parent 3cab703 commit a0b0e1c

7 files changed

Lines changed: 120 additions & 7 deletions

File tree

core-java/pom.xml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
<parent>
1111
<groupId>com.baeldung</groupId>
1212
<artifactId>parent-java</artifactId>
13-
<version>0.0.1-SNAPSHOT</version>
14-
<relativePath>../parent-java</relativePath>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<relativePath>../parent-java</relativePath>
1515
</parent>
1616

1717
<dependencies>
@@ -198,11 +198,6 @@
198198
<artifactId>mail</artifactId>
199199
<version>${javax.mail.version}</version>
200200
</dependency>
201-
<dependency>
202-
<groupId>javax.mail</groupId>
203-
<artifactId>mail</artifactId>
204-
<version>1.5.0-b01</version>
205-
</dependency>
206201
</dependencies>
207202

208203
<build>
@@ -250,6 +245,7 @@
250245
<plugin>
251246
<groupId>org.apache.maven.plugins</groupId>
252247
<artifactId>maven-jar-plugin</artifactId>
248+
<version>${maven-jar-plugin.version}</version>
253249
<configuration>
254250
<archive>
255251
<manifest>
@@ -288,6 +284,7 @@
288284
<plugin>
289285
<groupId>org.apache.maven.plugins</groupId>
290286
<artifactId>maven-shade-plugin</artifactId>
287+
<version>${maven-shade-plugin.version}</version>
291288
<executions>
292289
<execution>
293290
<goals>
@@ -309,6 +306,7 @@
309306
<plugin>
310307
<groupId>com.jolira</groupId>
311308
<artifactId>onejar-maven-plugin</artifactId>
309+
<version>${onejar-maven-plugin.version}</version>
312310
<executions>
313311
<execution>
314312
<configuration>
@@ -326,6 +324,7 @@
326324
<plugin>
327325
<groupId>org.springframework.boot</groupId>
328326
<artifactId>spring-boot-maven-plugin</artifactId>
327+
<version>${spring-boot-maven-plugin.version}</version>
329328
<executions>
330329
<execution>
331330
<goals>
@@ -473,6 +472,11 @@
473472
<maven-javadoc-plugin.version>3.0.0-M1</maven-javadoc-plugin.version>
474473
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
475474
<javax.mail.version>1.5.0-b01</javax.mail.version>
475+
<maven-jar-plugin.version>3.0.2</maven-jar-plugin.version>
476+
<onejar-maven-plugin.version>1.4.4</onejar-maven-plugin.version>
477+
<maven-shade-plugin.version>3.1.1</maven-shade-plugin.version>
478+
<spring-boot-maven-plugin.version>2.0.3.RELEASE</spring-boot-maven-plugin.version>
479+
476480
</properties>
477481

478482
</project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.customexception;
2+
3+
import java.io.File;
4+
import java.io.FileNotFoundException;
5+
import java.util.Scanner;
6+
7+
public class FileManager {
8+
9+
public static String getFirstLine(String fileName) throws IncorrectFileNameException {
10+
try (Scanner file = new Scanner(new File(fileName))) {
11+
if (file.hasNextLine()) {
12+
return file.nextLine();
13+
} else {
14+
throw new IllegalArgumentException("Non readable file");
15+
}
16+
} catch (FileNotFoundException err) {
17+
if (!isCorrectFileName(fileName)) {
18+
throw new IncorrectFileNameException("Incorrect filename : " + fileName, err);
19+
}
20+
// Logging etc
21+
} catch (IllegalArgumentException err) {
22+
if (!containsExtension(fileName)) {
23+
throw new IncorrectFileExtensionException("Filename does not contain extension : " + fileName, err);
24+
}
25+
// Other error cases and logging
26+
}
27+
return "Default First Line";
28+
}
29+
30+
private static boolean containsExtension(String fileName) {
31+
if (fileName.contains(".txt") || fileName.contains(".doc"))
32+
return true;
33+
return false;
34+
}
35+
36+
private static boolean isCorrectFileName(String fileName) {
37+
if (fileName.equals("wrongFileName.txt"))
38+
return false;
39+
else
40+
return true;
41+
}
42+
43+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.customexception;
2+
3+
public class IncorrectFileExtensionException extends RuntimeException{
4+
private static final long serialVersionUID = 1L;
5+
6+
public IncorrectFileExtensionException(String errorMessage, Throwable err) {
7+
super(errorMessage, err);
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.customexception;
2+
3+
public class IncorrectFileNameException extends Exception {
4+
private static final long serialVersionUID = 1L;
5+
6+
public IncorrectFileNameException(String errorMessage, Throwable err) {
7+
super(errorMessage, err);
8+
}
9+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.customexception;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.io.File;
6+
7+
import org.junit.Test;
8+
9+
public class IncorrectFileExtensionExceptionUnitTest {
10+
11+
@Test
12+
public void testWhenCorrectFileExtensionGiven_ReceivesNoException() throws IncorrectFileNameException {
13+
assertThat(FileManager.getFirstLine("correctFileNameWithProperExtension.txt")).isEqualTo("Default First Line");
14+
}
15+
16+
@Test(expected = IncorrectFileExtensionException.class)
17+
public void testWhenCorrectFileNameExceptionThrown_ReceivesNoException() throws IncorrectFileNameException {
18+
StringBuffer sBuffer = new StringBuffer();
19+
sBuffer.append("src");
20+
sBuffer.append(File.separator);
21+
sBuffer.append("test");
22+
sBuffer.append(File.separator);
23+
sBuffer.append("resources");
24+
sBuffer.append(File.separator);
25+
sBuffer.append("correctFileNameWithoutProperExtension");
26+
FileManager.getFirstLine(sBuffer.toString());
27+
}
28+
29+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.customexception;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.Test;
6+
7+
public class IncorrectFileNameExceptionUnitTest {
8+
9+
@Test(expected = IncorrectFileNameException.class)
10+
public void testWhenIncorrectFileNameExceptionThrown_ReceivesIncorrectFileNameException() throws IncorrectFileNameException {
11+
FileManager.getFirstLine("wrongFileName.txt");
12+
}
13+
14+
@Test
15+
public void testWhenCorrectFileNameExceptionThrown_ReceivesNoException() throws IncorrectFileNameException {
16+
assertThat(FileManager.getFirstLine("correctFileName.txt")).isEqualTo("Default First Line");
17+
}
18+
19+
}

core-java/src/test/resources/correctFileNameWithoutProperExtension

Whitespace-only changes.

0 commit comments

Comments
 (0)