Your last solution, to use specific exceptions, would be more readable but could result in a lot of Exception subclass.

Here is a quick code update :

public static void main(String[] args) {
    try {
        String filePath = getFilePath(args);
        AtomicReference<String> content = new AtomicReference<String>();
        content.set(readYamlFile(sourceFilePath));
        jsonStr = convert(content.toString());
    }  catch (FileNotFoundException e) {
        System.out.println("not found");
    } catch (ReadYamlFileException e) {
        System.out.println("Not able to load file");
    } catch (ConvertJsonExceptione) {
        System.out.println("Conversion failed ");
    }
}

private String readYamlFile(String) throws ReadYamlFileException{ ... }
private String convert(String) throws ConvertJsonException{ ... }

Since I try to have the return statements in the beginning (filter parameter) or the end of a method (correct method execution), this design is useful. If not, we would have multiple line that could stop a process, leading to a complicated debugging.

EDIT:

Of course, if you only need to print a text and stop there (no exception management), you can simply create your own exception in each method :

private String readYamlFile(String) throws IOException {
    try{
       ...
    } catch (IOException e) {
        throw new IOException("Not able to load file", e);
    }
}

Where e is the original Exception thrown

Answer from AxelH on Stack Overflow
🌐
JetBrains
blog.jetbrains.com › idea › 2024 › 03 › easy-hacks-how-to-throw-java-exceptions
Easy Hacks: How to Throw Java Exceptions | The IntelliJ IDEA Blog
March 12, 2024 - When an exception occurs, the Java runtime stops the execution of the current method and passes the exception object to the nearest catch block that can handle it. To throw an exception, you can use the throw keyword followed by the exception object.
🌐
JanBask Training
janbasktraining.com › community › java › does-throwing-an-exception-stop-execution-java
Does throwing an exception stop execution java? | JanBask Training Community
October 10, 2022 - The answer to your question - Does throwing an exception stop execution java is : I would say that this isn't controlling the flow of your application.
Top answer
1 of 5
3

Your last solution, to use specific exceptions, would be more readable but could result in a lot of Exception subclass.

Here is a quick code update :

public static void main(String[] args) {
    try {
        String filePath = getFilePath(args);
        AtomicReference<String> content = new AtomicReference<String>();
        content.set(readYamlFile(sourceFilePath));
        jsonStr = convert(content.toString());
    }  catch (FileNotFoundException e) {
        System.out.println("not found");
    } catch (ReadYamlFileException e) {
        System.out.println("Not able to load file");
    } catch (ConvertJsonExceptione) {
        System.out.println("Conversion failed ");
    }
}

private String readYamlFile(String) throws ReadYamlFileException{ ... }
private String convert(String) throws ConvertJsonException{ ... }

Since I try to have the return statements in the beginning (filter parameter) or the end of a method (correct method execution), this design is useful. If not, we would have multiple line that could stop a process, leading to a complicated debugging.

EDIT:

Of course, if you only need to print a text and stop there (no exception management), you can simply create your own exception in each method :

private String readYamlFile(String) throws IOException {
    try{
       ...
    } catch (IOException e) {
        throw new IOException("Not able to load file", e);
    }
}

Where e is the original Exception thrown

2 of 5
3

I want to stop if exception occurs

You can put return;, System.exit(1), or a throw e; within the catch blocks. Each will halt execution.

Depends on what you want, but you can use just one try block, followed by many catch blocks.

🌐
Rollbar
rollbar.com › home › how to throw exceptions in java
How to Throw Exceptions in Java | Rollbar
2 weeks ago - You place this inside a method when you detect something wrong. For instance, if validating user input fails or a required resource is missing, you throw an exception to stop execution and notify the calling code that something needs attention.
Top answer
1 of 2
3

I figured out what and how to do what I was needing. What I needed to stop the creation of the routine object if conditions were met, and that was done by throwing an error. That was being done, but I was mistakenly setting the variables INSIDE the try...catch block. So, if the conditions are not satisfied, it fails and stops, if they are satisfied, it creates the object and sets the variables OUTSIDE of the try...catch block, which works. My new code is as follows:

package routines;

import java.io.IOException;
import java.util.Random;

import game.*;

public class MovePlayer extends Routine {

    private final int destX;
    private final int destY;
    private final Random random = new Random();

    public MovePlayer(int destX, int destY, GameBoard board) throws IOException {
        super();
        try {
            if (destY > board.getHeight() || destX > board.getWidth()) {
                throw new IllegalArgumentException(">>> Error while creating routine, one or more coords are outside of the game board");
            } else {
            }
        } catch (IllegalArgumentException e) {
            fail();
        System.err.println(e.getLocalizedMessage());
    }
    this.destX = destX;
    this.destY = destY;
}
2 of 2
1

By using the

try{

}
catch(Exception){

}
finally{

}

block you can handle exceptions instead of letting them crash your program!

Your code comes in the try block, and in the catch block you can handle the Exception which is being thrown from the executing code in the try-block.

The finally block is used to close or finalize used resources in order to not leave them opened. Not doing that might - ironically - throw you even more exceptions.

I would definitely look up on that, it's a very commonly used part of modern languages!

🌐
Jenkov
tutorials.jenkov.com › java-exception-handling › basic-try-catch-finally.html
Basic try-catch-finally Exception Handling in Java
All methods in the call stack between the method throwing the exception and the method catching it have their execution stopped at the point in the code where the exception is thrown or propagated.
Find elsewhere
🌐
Java Programming
java-programming.mooc.fi › part-11 › 3-exceptions
Exceptions - Java Programming
We use the keyword catch, because causing an exception is referred to as throwing an exception. As mentioned above, we do not have to prepare for runtime exceptions such as the NullPointerException. We do not have to handle these kinds of exceptions, so the program execution stops if an error causes the exception to be thrown.
🌐
Reddit
reddit.com › r/askprogramming › is there a difference between throwing an exception and using a try catch with a print( that states the error obtained) in java?
r/AskProgramming on Reddit: Is there a difference between throwing an exception and using a try catch with a print( that states the error obtained) in Java?
September 30, 2023 - Throwing exception just passes control back to the calling stack. It's possible that the other method that called you (be it from same or a different package) might have well handled that error in some form.
Top answer
1 of 4
2

speakToPerson2() is catching the Exception, but you want communicate() to catch it too. Essentially, speakToPerson2() is suppressing the exception, so no other method sees it.

What you need to do is rethrow it (in both speakToPerson2() and speakToPerson3()). This is known as propagating an exception.

I.e.:

public static void speakToPerson2() {
    try {
      speakToPerson1();
    } catch (Exception e) {
      System.out.println("speakToPerson2 caused exception");
      e.printStackTrace();
      throw e;
    }
  }

  public static void speakToPerson3() {
    try {
      speakToPerson2();

    } catch (Exception e) {
      System.out.println("speakToPerson3 caused exception");
      e.printStackTrace();
      throw e;
    }
  }
2 of 4
2

You need to understand exactly how try-catch work. If any method in the try-catch throws an exception (one that extends/or is Exception in your example), the body of the try-catch will be interrupted to go into the catch clause.

In your case, the method speakToPerson1 will throw a TryToSpeakException. This exception will be forwarded one step above in the method call stack, the method speakToPerson2 in your case. Since the call to speakToPerson1 is surrounded with a try-catch, the catch clause is invoked and System.out.println("speakToPerson2 caused exception"); is executed. Now, the try clause encloses two methods calls, namely speakToPerson1 and keepSilentToPerson. However, since the first method throw the exception, the second is never reached and therefore keepSilentToPerson() will never by called.

Finally, think about the catching of exceptions. If you catch an exception, you are saying that you are going to handle it, by recovering or rethrowing it. If you handle it without rethrowing it, it won't be forwarded to the upper level of your call stack. Beware of this technicality

🌐
Oracle
docs.oracle.com › javase › tutorial › essential › exceptions › throwing.html
How to Throw Exceptions (The Java™ Tutorials > Essential Java Classes > Exceptions)
For now, all you need to remember is that you can throw only objects that inherit from the java.lang.Throwable class. Note that the declaration of the pop method does not contain a throws clause. EmptyStackException is not a checked exception, so pop is not required to state that it might occur. The objects that inherit from the Throwable class include direct descendants (objects that inherit directly from the Throwable class) and indirect descendants (objects that inherit from children or grandchildren of the Throwable class).
🌐
GeeksforGeeks
geeksforgeeks.org › java › throw-throws-java
throw and throws in Java - GeeksforGeeks
The flow of execution of the program stops immediately after the throw statement is executed and the nearest enclosing try block is checked to see if it has a catch statement that matches the type of exception.
Published   August 5, 2025
🌐
Quora
quora.com › In-Java-when-you-use-a-Throw-Exception-are-you-skipping-over-bad-code-to-allow-the-program-to-continue-running-or-when-would-it-be-used-for
In Java, when you use a Throw Exception, are you skipping over bad code to allow the program to continue running or when would it be used for? - Quora
Answer (1 of 3): Here’s an excellent writeup on the use and misuse of exceptions: Vexing exceptions Personally, I tend to catch exception in one of two places: immediate outside the function that throws it (these would be the ‘vexing’ exceptions from the above article), or at the very ...