How to Make Program to Continue Running After Exception

How to make program to continue running after exception?

Catch the exception:

public void run() {
if (!isLoading && !playerList.isEmpty()) {
this.isLoading = true;
ArrayList<Player> clonedList = (ArrayList<Player>)playerList.clone();
playerList.clear();
for (Player player : clonedList) {
try {
main.getDataManager().loadPlayer(player);
} catch (Exception e) { }
}
this.isLoading = false;
}
}

You can alternatively put the whole for loop in the try-catch block, so that if one player fails to load, it stops trying to load others until run is called again.

public void run() {
if (!isLoading && !playerList.isEmpty()) {
this.isLoading = true;
ArrayList<Player> clonedList = (ArrayList<Player>)playerList.clone();
playerList.clear();
try {
for (Player player : clonedList) {
main.getDataManager().loadPlayer(player);
}
} catch (Exception e) { }
this.isLoading = false;
}
}

Continue program after exception

You are using throw to raise an exception but if you want to handle the exception you should use try and catch block. See this answer to know what happens when you don't use try and catch block.

Use try block where there is possibility to occur an exception.

In your code, it is happening in if-else if-else block.

So,

while (guess != number) {
System.out.print("Enter your guess: ");
guess = scanner.nextInt();
try {
if (guess < number) {
throw new TooLowException();
} else if (guess > number) {
throw new TooHighException();
} else {
throw new CorrectException();
}
} // end the try block
}

Now you want to handle the exception and not bubble up, so you need to use catch block immediately after try block.

So,

while (guess != number) {
System.out.print("Enter your guess: ");
guess = scanner.nextInt();
try {
if (guess < number) {
throw new TooLowException();
} else if (guess > number) {
throw new TooHighException();
} else {
throw new CorrectException();
}
} catch(Exception e) {
System.out.println(e);
}
}

When you are using Exceptions created by you, it is a good thing to mention what is the exception is about like what is the cause when the exception is raised.

To do that, override toString() of Exception class. You don't have to write the message to user in catch block everytime if you are using your custom exception classes multiple times in your code.


Edit:

import java.util.Random;
import java.util.Scanner;
class TooHighException extends Exception {
}
class TooLowException extends Exception {
}
class CorrectException extends Exception {
}
public class HighLow {
/**
* @param args the command line arguments
* @throws TooLowException
* @throws TooHighException
* @throws CorrectException
*/
public static void main(String[] args) {
Random random = new Random();
Scanner scanner = new Scanner(System.in);
int number = random.nextInt(100);
int guess = -1;
while (guess != number) {
System.out.print("Enter your guess: ");
guess = scanner.nextInt();
try {
if (guess < number) {
throw new TooLowException();
} else if (guess > number) {
throw new TooHighException();
} else {
throw new CorrectException();
}
} catch(Exception e) {
System.out.println(e);
}
}
} // end main
} // end class

Java - How to keep running on exception

Use a try catch block.

try{
//Where exception may happen
}catch(Exception e){//Exception type. Exception covers it all.
//Print error if you would like or do something else
}finally{//Finally is optional, as the code in here will run regardless of an exception.
}
//program continues

Most try-catch blocks do not have finally at the end. You will use finally if you need code to be run whether or not there was an exception. More information about the finally block

How to keep the code running even after exception

You should use a try-catch inside your loop:

public static void main(String[] args){
for (int i=0; i<10; i++) {
try {
//your code goes here
} catch (ExceptionType name) {
//error-handling code
}
}
}

Learn more about it here.



Related Topics



Leave a reply



Submit