Skip to content

Commit a8999da

Browse files
author
coursar
committed
feat(exceptions)
1 parent 49a5cc8 commit a8999da

File tree

12 files changed

+337
-1
lines changed

12 files changed

+337
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
## Блок 4. Исключения, Интерфейсы, Generics и Collections Framework
3434

35-
4.1. [ ] [Исключительные ситуации и их обработка. Тестирование исключений](exceptions)
35+
4.1. [x] [Исключительные ситуации и их обработка. Тестирование исключений](exceptions)
3636

3737
4.2. [ ] [Интерфейсы для организации малой связности. Обобщённое программирование (Generics)](interfaces)
3838

exceptions/exception-types/pom.xml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>ru.netology</groupId>
8+
<artifactId>exception-types</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>11</maven.compiler.source>
13+
<maven.compiler.target>11</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.projectlombok</groupId>
20+
<artifactId>lombok</artifactId>
21+
<version>1.18.12</version>
22+
<scope>provided</scope>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.junit.jupiter</groupId>
26+
<artifactId>junit-jupiter</artifactId>
27+
<version>5.4.2</version>
28+
<scope>test</scope>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.mockito</groupId>
32+
<artifactId>mockito-junit-jupiter</artifactId>
33+
<version>3.3.3</version>
34+
<scope>test</scope>
35+
</dependency>
36+
</dependencies>
37+
38+
<build>
39+
<plugins>
40+
<plugin>
41+
<groupId>org.apache.maven.plugins</groupId>
42+
<artifactId>maven-surefire-plugin</artifactId>
43+
<version>2.22.2</version>
44+
</plugin>
45+
</plugins>
46+
</build>
47+
48+
</project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import exception.CheckedException;
2+
import service.Service;
3+
4+
public class Main {
5+
public static void main(String[] args) {
6+
Service service = new Service();
7+
8+
try {
9+
service.throwChecked();
10+
} catch (CheckedException e) {
11+
e.printStackTrace();
12+
}
13+
14+
service.throwUnchecked();
15+
}
16+
}
17+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package exception;
2+
3+
public class CheckedException extends Exception {
4+
public CheckedException() {
5+
super();
6+
}
7+
8+
public CheckedException(String message) {
9+
super(message);
10+
}
11+
12+
public CheckedException(String message, Throwable cause) {
13+
super(message, cause);
14+
}
15+
16+
public CheckedException(Throwable cause) {
17+
super(cause);
18+
}
19+
20+
protected CheckedException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
21+
super(message, cause, enableSuppression, writableStackTrace);
22+
}
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package exception;
2+
3+
public class UncheckedException extends RuntimeException {
4+
public UncheckedException() {
5+
}
6+
7+
public UncheckedException(String message) {
8+
super(message);
9+
}
10+
11+
public UncheckedException(String message, Throwable cause) {
12+
super(message, cause);
13+
}
14+
15+
public UncheckedException(Throwable cause) {
16+
super(cause);
17+
}
18+
19+
public UncheckedException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
20+
super(message, cause, enableSuppression, writableStackTrace);
21+
}
22+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package service;
2+
3+
import exception.CheckedException;
4+
import exception.UncheckedException;
5+
6+
public class Service {
7+
public void throwChecked() throws CheckedException {
8+
throw new CheckedException();
9+
}
10+
11+
public void throwUnchecked() {
12+
throw new UncheckedException();
13+
}
14+
}
15+
16+
17+
18+
19+
20+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package service;
2+
3+
import exception.CheckedException;
4+
import exception.UncheckedException;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.*;
8+
9+
public class ServiceTest {
10+
private Service service = new Service();
11+
12+
@Test
13+
public void shouldThrowCheckedException() {
14+
assertThrows(CheckedException.class, () -> service.throwChecked());
15+
}
16+
17+
@Test
18+
public void shouldThrowUncheckedException() {
19+
assertThrows(UncheckedException.class, () -> service.throwUnchecked());
20+
}
21+
}
22+
23+

exceptions/exceptions/pom.xml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>ru.netology</groupId>
8+
<artifactId>exceptions</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>11</maven.compiler.source>
13+
<maven.compiler.target>11</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.projectlombok</groupId>
20+
<artifactId>lombok</artifactId>
21+
<version>1.18.12</version>
22+
<scope>provided</scope>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.junit.jupiter</groupId>
26+
<artifactId>junit-jupiter</artifactId>
27+
<version>5.4.2</version>
28+
<scope>test</scope>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.mockito</groupId>
32+
<artifactId>mockito-junit-jupiter</artifactId>
33+
<version>3.3.3</version>
34+
<scope>test</scope>
35+
</dependency>
36+
</dependencies>
37+
38+
<build>
39+
<plugins>
40+
<plugin>
41+
<groupId>org.apache.maven.plugins</groupId>
42+
<artifactId>maven-surefire-plugin</artifactId>
43+
<version>2.22.2</version>
44+
</plugin>
45+
</plugins>
46+
</build>
47+
48+
</project>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package ru.netology;
2+
3+
import ru.netology.domain.PurchaseItem;
4+
import ru.netology.manager.CartManager;
5+
import ru.netology.repository.CartRepository;
6+
7+
public class Main {
8+
public static void main(String[] args) {
9+
CartManager manager = new CartManager(new CartRepository());
10+
manager.add(new PurchaseItem(
11+
1,
12+
1,
13+
"Java Core",
14+
1000,
15+
1
16+
));
17+
18+
// manager.removeById(2);
19+
// System.out.println("main done"); // for demo only
20+
21+
// try {
22+
// System.out.println("before remove");
23+
// manager.removeById(2);
24+
// System.out.println("after remove");
25+
// } catch (ArrayIndexOutOfBoundsException e) {
26+
// e.printStackTrace();
27+
// System.out.println("specific catch");
28+
// } catch (RuntimeException e) {
29+
// System.out.println("runtime catch");
30+
// } catch (Exception e) {
31+
// System.out.println("catch");
32+
// } finally {
33+
// System.out.println("finally");
34+
// }
35+
36+
try {
37+
System.out.println("before remove");
38+
manager.removeById(2);
39+
System.out.println("after remove");
40+
} catch (Throwable e) {
41+
42+
}
43+
44+
45+
46+
47+
System.out.println("main done"); // for demo only
48+
}
49+
}
50+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package ru.netology.domain;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
7+
@NoArgsConstructor
8+
@AllArgsConstructor
9+
@Data
10+
public class PurchaseItem {
11+
private int id;
12+
private int productId;
13+
private String productName;
14+
private int productPrice;
15+
private int count;
16+
}

0 commit comments

Comments
 (0)