Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

11 changes: 0 additions & 11 deletions core-java/src/main/java/com/baeldung/ExceptionHandling/Player.java

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package com.baeldung.exceptionhandling;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
Expand All @@ -10,9 +12,9 @@
import java.util.stream.Collectors;

public class Exceptions {

private final static Logger logger = Logger.getLogger("ExceptionLogging");

public static List<Player> getPlayers() throws IOException {
Path path = Paths.get("players.dat");
List<String> players = Files.readAllLines(path);
Expand All @@ -21,21 +23,20 @@ public static List<Player> getPlayers() throws IOException {
.map(Player::new)
.collect(Collectors.toList());
}



public List<Player> loadAllPlayers(String playersFile) throws IOException{
try {
throw new IOException();
} catch(IOException ex) {
throw new IllegalStateException();
}
}

public int getPlayerScoreThrows(String playerFile) throws FileNotFoundException {
Scanner contents = new Scanner(new File(playerFile));
return Integer.parseInt(contents.nextLine());
}

public int getPlayerScoreTryCatch(String playerFile) {
try {
Scanner contents = new Scanner(new File(playerFile));
Expand All @@ -44,7 +45,7 @@ public int getPlayerScoreTryCatch(String playerFile) {
throw new IllegalArgumentException("File not found");
}
}

public int getPlayerScoreTryCatchRecovery(String playerFile) {
try {
Scanner contents = new Scanner(new File(playerFile));
Expand All @@ -54,31 +55,28 @@ public int getPlayerScoreTryCatchRecovery(String playerFile) {
return 0;
}
}



public int getPlayerScoreFinally(String playerFile) throws FileNotFoundException {
Scanner contents = null;
try {
contents = new Scanner(new File(playerFile));
return Integer.parseInt(contents.nextLine());
} finally {
if (contents != null) {
contents.close();
if (contents != null) {
contents.close();
}
}
}
}



public int getPlayerScoreTryWithResources(String playerFile) {
try (Scanner contents = new Scanner(new File(playerFile))) {
return Integer.parseInt(contents.nextLine());
} catch (FileNotFoundException e ) {
logger.warning("File not found, resetting score.");
return 0;
}
}
}

public int getPlayerScoreMultipleCatchBlocks(String playerFile) {
try (Scanner contents = new Scanner(new File(playerFile))) {
return Integer.parseInt(contents.nextLine());
Expand All @@ -89,130 +87,126 @@ public int getPlayerScoreMultipleCatchBlocks(String playerFile) {
logger.warning("Player file was corrupted!");
return 0;
}
}


}

public int getPlayerScoreMultipleCatchBlocksAlternative(String playerFile) {
try (Scanner contents = new Scanner(new File(playerFile)) ) {
return Integer.parseInt(contents.nextLine());
} catch (FileNotFoundException e) {
logger.warning("Player file not found!");
return 0;
logger.warning("Player file not found!");
return 0;
} catch (IOException e) {
logger.warning("Player file wouldn't load!");
return 0;
} catch (NumberFormatException e) {
logger.warning("Player file was corrupted!");
return 0;
}
}
}

public int getPlayerScoreUnionCatchBlocks(String playerFile) {
try (Scanner contents = new Scanner(new File(playerFile))) {
return Integer.parseInt(contents.nextLine());
} catch (IOException | NumberFormatException e) {
logger.warning("Failed to load score!");
return 0;
}
}
}

public List<Player> loadAllPlayersThrowingChecked(String playersFile) throws TimeoutException {
boolean tooLong = true;

while (!tooLong) {
// ... potentially long operation
// ... potentially long operation
}
throw new TimeoutException("This operation took too long");
}
}

public List<Player> loadAllPlayersThrowingUnchecked(String playersFile) throws TimeoutException {
if(!isFilenameValid(playersFile)) {
throw new IllegalArgumentException("Filename isn't valid!");
}
return null;
// ...
}

// ...
}

public List<Player> loadAllPlayersWrapping(String playersFile) throws IOException {
try {
throw new IOException();
} catch (IOException io) {
throw io;
}
}
}

public List<Player> loadAllPlayersRethrowing(String playersFile) throws PlayerLoadException {
try {
throw new IOException();
} catch (IOException io) {
throw new PlayerLoadException(io);
}
}
}

public List<Player> loadAllPlayersThrowable(String playersFile) {
try {
throw new NullPointerException();
} catch ( Throwable t ) {
throw t;
}
}
class FewerExceptions extends Exceptions {
}

class FewerExceptions extends Exceptions {
@Override
public List<Player> loadAllPlayers(String playersFile) { //can't add "throws MyCheckedException
return null;
// overridden
}
}
}


public void throwAsGotoAntiPattern() {
try {
// bunch of code
throw new MyException();
// second bunch of code
} catch ( MyException e ) {
// third bunch of code
}
}
}
}

public int getPlayerScoreSwallowingExceptionAntiPattern(String playerFile) {
try {
// ...
} catch (Exception e) {} // <== catch and swallow
return 0;
}
}

public int getPlayerScoreSwallowingExceptionAntiPatternAlternative(String playerFile) {
try {
// ...
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}


}

public int getPlayerScoreSwallowingExceptionAntiPatternAlternative2(String playerFile) throws PlayerScoreException {
try {
throw new IOException();
} catch (IOException e) {
throw new PlayerScoreException(e);
}
}
}



public int getPlayerScoreReturnInFinallyAntiPattern(String playerFile) {
int score = 0;
try {
throw new IOException();
} finally {
return score; // <== the IOException is dropped
}
}
}


public boolean isFilenameValid(String name) {
private boolean isFilenameValid(String name) {
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.baeldung.exceptionhandling;

public class MyException extends Throwable {

}
12 changes: 12 additions & 0 deletions core-java/src/main/java/com/baeldung/exceptionhandling/Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.baeldung.exceptionhandling;

public class Player {

public int id;
public String name;

public Player(String name) {
this.name = name;
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.baeldung.exceptionhandling;

import java.io.IOException;

public class PlayerLoadException extends Exception {

public PlayerLoadException(IOException io) {
public PlayerLoadException(IOException io) {
super(io);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.baeldung.exceptionhandling;

public class PlayerScoreException extends Exception {

public PlayerScoreException(Exception e) {
super(e);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.baeldung.exceptionhandling;

public class TimeoutException extends Exception {

public TimeoutException(String message) {
super(message);
}
}
}
Loading