-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.pivovarit</groupId> | ||
<version>1.0</version> | ||
<artifactId>java-exception-stacktraces</artifactId> | ||
|
||
<name>java-exception-stacktraces</name> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>21</source> | ||
<target>21</target> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<version>2.19.1</version> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.junit.platform</groupId> | ||
<artifactId>junit-platform-surefire-provider</artifactId> | ||
<version>1.1.0</version> | ||
</dependency> | ||
</dependencies> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-engine</artifactId> | ||
<version>5.10.2</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
<version>3.22.0</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
23 changes: 23 additions & 0 deletions
23
java-exception-stacktraces/src/main/java/com/pivovarit/exception/ExceptionCacheExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.pivovarit.exception; | ||
|
||
import java.util.Collections; | ||
import java.util.IdentityHashMap; | ||
|
||
class ExceptionCacheExample { | ||
|
||
public static void main(String[] args) { | ||
var exceptions = Collections.newSetFromMap(new IdentityHashMap<>()); | ||
|
||
String foo = null; | ||
for (int i = 0; i < Integer.MAX_VALUE; i++) { | ||
try { | ||
foo.toUpperCase(); | ||
} catch (NullPointerException e) { | ||
exceptions.add(e); | ||
} | ||
} | ||
99327 | ||
|
||
System.out.println(exceptions.size()); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
java-exception-stacktraces/src/main/java/com/pivovarit/exception/StacktraceDropExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.pivovarit.exception; | ||
|
||
import java.util.Arrays; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
class StacktraceDropExample { | ||
|
||
public static void main(String[] args) { | ||
var previous = new AtomicReference<NullPointerException>(); | ||
|
||
String foo = null; | ||
for (int i = 0; i < Integer.MAX_VALUE; i++) { | ||
try { | ||
foo.toUpperCase(); | ||
} catch (NullPointerException e) { | ||
if (e.getStackTrace().length == 0) { | ||
System.out.printf("Stacktrace dropped at iteration %d%n", i); | ||
System.out.printf("Last stacktrace: %s%n", | ||
Arrays.toString(previous.get().getStackTrace())); | ||
System.out.printf("New stacktrace: %s%n", | ||
Arrays.toString(e.getStackTrace())); | ||
return; | ||
} | ||
previous.set(e); | ||
} | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...on-stacktraces/src/main/java/com/pivovarit/exception/StaticStacklessExceptionExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.pivovarit.exception; | ||
|
||
class StaticStacklessExceptionExample { | ||
|
||
private static final NullPointerException NULL_POINTER_EXCEPTION = new NullPointerException(); | ||
|
||
static { | ||
NULL_POINTER_EXCEPTION.setStackTrace(new StackTraceElement[0]); | ||
} | ||
|
||
public static void main(String[] args) { | ||
throw NULL_POINTER_EXCEPTION; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters