From 8314651a0121b58f1124b89dc3b2d61df5c8f71e Mon Sep 17 00:00:00 2001 From: Kent Date: Sat, 21 Dec 2024 01:00:36 +0100 Subject: [PATCH] [null-in-exception.msg] stack trace --- .../core-java-exceptions-5/README.md | 5 + .../core-java-exceptions-5/pom.xml | 15 +++ .../ExceptionGetMessageNullUnitTest.java | 110 ++++++++++++++++++ core-java-modules/pom.xml | 1 + 4 files changed, 131 insertions(+) create mode 100644 core-java-modules/core-java-exceptions-5/README.md create mode 100644 core-java-modules/core-java-exceptions-5/pom.xml create mode 100644 core-java-modules/core-java-exceptions-5/src/test/java/com/baeldung/exception/nullmessage/ExceptionGetMessageNullUnitTest.java diff --git a/core-java-modules/core-java-exceptions-5/README.md b/core-java-modules/core-java-exceptions-5/README.md new file mode 100644 index 000000000000..c513bf4de29c --- /dev/null +++ b/core-java-modules/core-java-exceptions-5/README.md @@ -0,0 +1,5 @@ +## Core Java Exceptions + +This module contains articles about core java exceptions + +### Relevant articles: diff --git a/core-java-modules/core-java-exceptions-5/pom.xml b/core-java-modules/core-java-exceptions-5/pom.xml new file mode 100644 index 000000000000..4e49609a9739 --- /dev/null +++ b/core-java-modules/core-java-exceptions-5/pom.xml @@ -0,0 +1,15 @@ + + + 4.0.0 + core-java-exceptions-5 + jar + core-java-exceptions-5 + + + com.baeldung.core-java-modules + core-java-modules + 0.0.1-SNAPSHOT + + diff --git a/core-java-modules/core-java-exceptions-5/src/test/java/com/baeldung/exception/nullmessage/ExceptionGetMessageNullUnitTest.java b/core-java-modules/core-java-exceptions-5/src/test/java/com/baeldung/exception/nullmessage/ExceptionGetMessageNullUnitTest.java new file mode 100644 index 000000000000..fcd111486143 --- /dev/null +++ b/core-java-modules/core-java-exceptions-5/src/test/java/com/baeldung/exception/nullmessage/ExceptionGetMessageNullUnitTest.java @@ -0,0 +1,110 @@ +package com.baeldung.exception.nullmessage; + +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +class Team { + + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} + +class Player { + + private static Logger LOG = LoggerFactory.getLogger(Player.class); + private String name; + private Team team; + + public Player(String name) { + this.name = name; + } + + public void output() { + try { + if (team != null) { + LOG.info("Player: {}, Team: {}", name.toUpperCase(), team.getName().toUpperCase()); + } else { + throw new IllegalArgumentException("Play's team is null"); + } + } catch (Exception e) { + LOG.error("Error occurred." + e.getMessage()); + } + } + + public void outputWithStackTrace() { + try { + if (team != null) { + LOG.info("Player: {}, Team: {}", name.toUpperCase(), team.getName().toUpperCase()); + } else { + throw new IllegalArgumentException("Play's team is null"); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + public void outputWithStackTraceLog() { + try { + if (team != null) { + LOG.info("Player: {}, Team: {}", name.toUpperCase(), team.getName().toUpperCase()); + } else { + throw new IllegalArgumentException("Play's team is null"); + } + } catch (Exception e) { + LOG.error("Error occurred.", e); + } + } + + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Team getTeam() { + return team; + } + + public void setTeam(Team team) { + this.team = team; + } + +} + +public class ExceptionGetMessageNullUnitTest { + + @Test + void whenCallingPlayerOutput_thenGetExceptionWithNullMessage() { + Player kai = new Player("Kai"); + kai.setTeam(new Team()); + + kai.output(); + } + + @Test + void whenCallingPlayerOutputWithStackTrace_thenGetStackTrace() { + Player kai = new Player("Kai"); + kai.setTeam(new Team()); + + kai.outputWithStackTrace(); + } + + @Test + void whenCallingPlayerOutputWithStackTraceLog_thenGetStackTrace() { + Player kai = new Player("Kai"); + kai.setTeam(new Team()); + + kai.outputWithStackTraceLog(); + } +} diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml index 38c00570a6e8..a953e2cebea7 100644 --- a/core-java-modules/pom.xml +++ b/core-java-modules/pom.xml @@ -128,6 +128,7 @@ core-java-exceptions-2 core-java-exceptions-3 + core-java-exceptions-5 core-java-function core-java-functional core-java-hex