Explanation

From the Java documentation:

[The try block] contains one or more legal lines of code that could throw an exception. (The catch and finally blocks are explained in the next two subsections.)

An exception is a special kind of object. When you write new Exception(), you are creating a new exception object. When you write throw new Exception() you are creating a new error, and then throwing it to the nearest try-catch block, aborting the rest of your code.

When you throw an exception, it gets caught by the try-catch block that it's nested in (inside of). That is, assuming the proper catch block for that exception is registered. If the code is not wrapped in a try-catch block, the program with automatically shut down as soon as an error is thrown. Use a try-catch around any code or method that can throw an error, especially because of user input (within reason).

Some exceptions have to be caught, others are optional to catch. (checked vs. unchecked).

When you add throws to a method signature, you are announcing to other methods that if they call that method, it has the potential to throw a checked exception (it is not necessary for unchecked). Notice how it's throws not throw. It's not doing an action, it's describing that it sometimes does an action.

You use this functionality when you don't want to catch the error inside that method, but want to allow the method's that call your method to catch the error themselves.

Exceptions are a way to make your program respond coherently to unexpected or invalid situations and are especially useful when user input is required, though it's also useful in other situations such as File input/output.

Examples

public CircleWithException() throws InvalidRadiusException {
       this(1.0);
}

Here, the CircleWithException() has the potential to throw an InvalidRadiusException (presumably, the this(1.0) sometimes throws an InvalidRadiusException.)

The code calling this method should have:

try {
    new CircleWithException(); // This calls the method above
} catch (InvalidRadiusException e) { // The object "e" is the exception object that was thrown.
    // this is where you handle it if an error occurs
}

As I said before, an Exception is just a specific type of object that extends Exception

/* Invalid radius class that contains error code */
public class InvalidRadiusException extends Exception {
     private double radius;

/** Construct an exception */
public InvalidRadiusException(double radius) {
       super("Invalid radius " + radius);
       this.radius = radius;
}

/** Return the radius */
public double getRadius() {
    return radius;
 }
}

The above code defines a new type of Exception specific to your program/application. There are many predefined exceptions in the Java Standard Library, but often you need to create your own.

To throw this exception, you first create an InvalidRadiusException object and then throw it:

throw new InvalidRadiusException(1.0);
Answer from sinθ on Stack Overflow
🌐
W3Schools
w3schools.com › java › java_try_catch.asp
Java Exceptions (Try...Catch)
Exception handling lets you catch and handle errors during runtime - so your program doesn't crash. ... The try statement allows you to define a block of code to be tested for errors while it is being executed.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-try-catch-block
Java Try Catch Block - GeeksforGeeks
June 3, 2025 - A try-catch block in Java is a mechanism to handle exceptions. This make sure that the application continues to run even if an error occurs.
Discussions

Try/Catch inside Try/Catch? (Java Netbeans)
Show the code More on reddit.com
🌐 r/learnprogramming
11
2
June 11, 2023
Does anyone use the catch and try keywords on java?
You're looking at this though an extremely narrow lens. try/catch isn't for debugging purposes. It's so you can handle errors programmatically. They're useful when you don't want your program to just crash and print an error message. For example, if I was writing an application that had to call some external web API, I don't want the program to crash if that API is offline. I want to try to call the API, and catch any errors, so I can display a nice error message to the user and inform them of the problem, instead of just having the program crash and the user have no idea what to do about it. In short, you're thinking about this from a "how can I figure out what this error is", but the point of the feature is "I know that an error could happen here, this is the code to handle that error". More on reddit.com
🌐 r/learnprogramming
35
7
September 6, 2022
java - What to put in a try/catch? - Software Engineering Stack Exchange
Note on the question: this is not a duplicate, Efficient try / catch block usage? was asked after this one. The other question is the duplicate. I was wondering what was the best way to use try/ca... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
June 9, 2011
Try/Catch vs Throws?
The try/catch block catches exceptions that are thrown, so they pretty much do opposite things. The throw exception says "Hey, here's an exception" and the try/catch block decides what to do with it (which can include printing an error message, throwing the exception again, throwing a slightly different exception, or handling the exception and continuing normally). More on reddit.com
🌐 r/learnprogramming
10
35
December 9, 2015
Top answer
1 of 4
10

Explanation

From the Java documentation:

[The try block] contains one or more legal lines of code that could throw an exception. (The catch and finally blocks are explained in the next two subsections.)

An exception is a special kind of object. When you write new Exception(), you are creating a new exception object. When you write throw new Exception() you are creating a new error, and then throwing it to the nearest try-catch block, aborting the rest of your code.

When you throw an exception, it gets caught by the try-catch block that it's nested in (inside of). That is, assuming the proper catch block for that exception is registered. If the code is not wrapped in a try-catch block, the program with automatically shut down as soon as an error is thrown. Use a try-catch around any code or method that can throw an error, especially because of user input (within reason).

Some exceptions have to be caught, others are optional to catch. (checked vs. unchecked).

When you add throws to a method signature, you are announcing to other methods that if they call that method, it has the potential to throw a checked exception (it is not necessary for unchecked). Notice how it's throws not throw. It's not doing an action, it's describing that it sometimes does an action.

You use this functionality when you don't want to catch the error inside that method, but want to allow the method's that call your method to catch the error themselves.

Exceptions are a way to make your program respond coherently to unexpected or invalid situations and are especially useful when user input is required, though it's also useful in other situations such as File input/output.

Examples

public CircleWithException() throws InvalidRadiusException {
       this(1.0);
}

Here, the CircleWithException() has the potential to throw an InvalidRadiusException (presumably, the this(1.0) sometimes throws an InvalidRadiusException.)

The code calling this method should have:

try {
    new CircleWithException(); // This calls the method above
} catch (InvalidRadiusException e) { // The object "e" is the exception object that was thrown.
    // this is where you handle it if an error occurs
}

As I said before, an Exception is just a specific type of object that extends Exception

/* Invalid radius class that contains error code */
public class InvalidRadiusException extends Exception {
     private double radius;

/** Construct an exception */
public InvalidRadiusException(double radius) {
       super("Invalid radius " + radius);
       this.radius = radius;
}

/** Return the radius */
public double getRadius() {
    return radius;
 }
}

The above code defines a new type of Exception specific to your program/application. There are many predefined exceptions in the Java Standard Library, but often you need to create your own.

To throw this exception, you first create an InvalidRadiusException object and then throw it:

throw new InvalidRadiusException(1.0);
2 of 4
3

You can declare a method to throw an exception if you can't (or it's not convinient) to handle the exception inside the method.

In your case, you are calling the method setRadius inside the constructor. If you think that is convinient to handle the exception (that is thrown by setRadius) inside the constructor, you can use a try-catch clause:

public CircleWithException(double newRadius) throws InvalidRadiusException {
    try {
        setRadius(newRadius);
        numberOfObjects++;
    } catch (InvalidRadiusException e) {
        setRadius(0); // for example
    }
}

The catch block contains what you want to do if an exception were thrown. In this case, I'm setting the radius to 0, but you can change this.

Remember that it depends in your classes implementation and how you want them to work. If you don't want the constructor to handle this exception, you can throw it (as you are already doing) and handle it in other methods.

🌐
CodeHS
codehs.com › tutorial › david › try-catch-in-java
Tutorial: Try Catch In Java | CodeHS
This tutorial covers the basics of try and catch statements in Java.
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › exceptions › catch.html
The catch Blocks (The Java™ Tutorials > Essential Java Classes > Exceptions)
The catch block contains code that is executed if and when the exception handler is invoked. The runtime system invokes the exception handler when the handler is the first one in the call stack whose ExceptionType matches the type of the exception thrown. The system considers it a match if ...
🌐
Programiz
programiz.com › java-programming › try-catch
Java try...catch (With Examples)
Here, we are trying to assign a value to the index 10. Hence, IndexOutOfBoundException occurs. ... The exception is thrown to the first catch block. The first catch block does not handle an IndexOutOfBoundsException, so it is passed to the next catch block. The second catch block in the above example is the appropriate exception handler because it handles an IndexOutOfBoundsException. Hence, it is executed. From Java SE 7 and later, we can now catch more than one type of exception with one catch block.
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › exceptions › try.html
The try Block (The Java™ Tutorials > Essential Java Classes > Exceptions)
If an exception occurs within the try block, that exception is handled by an exception handler associated with it. To associate an exception handler with a try block, you must put a catch block after it; the next section, The catch Blocks, shows you how.
Find elsewhere
🌐
Reddit
reddit.com › r/learnprogramming › try/catch inside try/catch? (java netbeans)
r/learnprogramming on Reddit: Try/Catch inside Try/Catch? (Java Netbeans)
June 11, 2023 -

This is probably going to be a big one, but I am 100% stuck here and my teacher has elected too offer no help whatsoever.

I as a beginner am currently trying to write a program that calculated "Tax Payable" based on earnings. To do so, I have to get an application to read lines from a .txt file, find the third part of each string separated by commas (part of an array, I think), convert it into a double and do calculations on it before writing the new information into a new document.

To do this we had been taught to have a Try/Catch for the read/write because otherwise it doesn't work, and a Try/Catch to try convert the String into a Double. Unfortunately, having a Try/Catch inside a Try/Catch just makes the code ignore the second instance of the command.

I've exhausted every idea I can think of and my teacher has completely refused to point me in the right direction. If you need to see the code please just let me know and I can add a comment with it in.

Thanks....

🌐
GeeksforGeeks
geeksforgeeks.org › java › exceptions-in-java
Java Exception Handling - GeeksforGeeks
Exceptions are events that occur during program execution that disrupt the normal flow of instructions. The try block contains code that might throw an exception, The catch block handles the exception if it occurs.
Published   3 weeks ago
🌐
Tutorialspoint
tutorialspoint.com › java › java_try_catch_block.htm
Java Try Catch Block
The previous statements demonstrate three catch blocks, but you can have any number of them after a single try. If an exception occurs in the protected code, the exception is thrown to the first catch block in the list. If the data type of the exception thrown matches ExceptionType1, it gets caught there.
🌐
BeginnersBook -
beginnersbook.com › home › java › try catch in java – exception handling
Try Catch in Java – Exception handling
May 30, 2024 - Try catch block is used for exception handling in Java. The code (or set of statements) that can throw an exception is placed inside try block and if the exception is raised, it is handled by the corresponding catch block. In this guide, we will see various examples to understand how to use ...
🌐
Reddit
reddit.com › r/learnprogramming › try/catch vs throws?
r/learnprogramming on Reddit: Try/Catch vs Throws?
December 9, 2015 -

Pretty new student to CS here. I'm curious what the point of throwing an exception is. Does it actually do anything?

Wouldn't the try/catch always be better as you catch for errors and actually deal with them in some way?

Top answer
1 of 5
8
The try/catch block catches exceptions that are thrown, so they pretty much do opposite things. The throw exception says "Hey, here's an exception" and the try/catch block decides what to do with it (which can include printing an error message, throwing the exception again, throwing a slightly different exception, or handling the exception and continuing normally).
2 of 5
7
I'm curious what the point of throwing an exception is. The point is to signal that an exceptional situation has occurred — a connection could not be made, a file does not exist, the specified key does not exist, the given index is out of bounds, etc. Does it actually do anything? Would would be the point if it didn't do anything? What happens when you throw an exception is out of your hands. It depends on all the stack frames above you, i.e. the person that called you, their caller, and their caller, and so on. Wouldn't the try/catch always be better as you catch for errors and actually deal with them in some way? I don't know what this means. throw is not an alternative to try and catch. The three are all related. Something has to be thrown (either by you, or code that you call) in order to have something to catch. If you can handle a specific kind of exception, then you should catch it and handle it. If you can't, you should either let the exception propagate upwards (i.e. do nothing), or you should catch and rethrow if the exception type needs to be translated into something that can be handled by somebody up the ladder from you.
🌐
Medium
medium.com › @AlexanderObregon › handling-basic-errors-with-try-and-catch-blocks-in-java-for-beginners-cf248fff543a
Handling Basic Errors With Try and Catch Blocks in Java for Beginners
June 13, 2025 - Now, if something goes wrong and an exception is thrown, Java stops the normal flow and starts looking for a catch block that knows how to handle that type of exception. It starts the search from the current method and works its way back down ...
🌐
DataCamp
datacamp.com › doc › java › try
try Keyword in Java: Usage & Examples
Optionally, a finally block can be used to execute code regardless of whether an exception was thrown or not. The try keyword is essential for exception handling in Java, helping to manage runtime errors and maintain the normal flow of the application. try { // Code that may throw an exception } catch (ExceptionType1 e1) { // Code to handle ExceptionType1 } catch (ExceptionType2 e2) { // Code to handle ExceptionType2 } finally { // Code that will always execute }
🌐
Zero To Mastery
zerotomastery.io › blog › try-catch-java
Beginner's Guide To Try And Catch In Java | Zero To Mastery
If the file doesn’t exist, Java throws a FileNotFoundException, and the catch block prints "File not found" The finally block runs no matter what happens and closes the scanner to free up system resources · Even if an error occurs, the scanner will always be closed. ... Without finally, you’d have to repeat cleanup code in both the try and catch blocks — which is inefficient and easy to forget.
🌐
Simplilearn
simplilearn.com › home › resources › software development › try catch in java
Try Catch in Java - Exception handling (With Examples) | Simplilearn
September 10, 2025 - Catch statements allow you to define a block of code that will be executed if an error occurs within a try block. Find out more about Try Catch in Java.
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
Medium
medium.com › @AlexanderObregon › java-exception-handling-throws-vs-try-catch-94b0abe1080d
Java Exception Handling — Throws vs. Try-Catch
March 17, 2024 - A try-catch block is made up of two main components: The try block: Contains the code that might throw an exception. By placing this code within a try block, you're indicating to the Java runtime that you want to attempt to execute this code, ...
🌐
Quora
quora.com › In-Java-should-throw-always-be-inside-a-try-block
In Java, should 'throw' always be inside a 'try' block? - Quora
Answer (1 of 5): Checked exceptions that extend java.lang.Exception are required to be inside try, and if not, must re-declare the exception in the method signature throws clauses. However, unchecked exceptions that extend RuntimeException are not required to be in try or rethrown. When they are...