It depends on whether you can deal with the exceptions that can be raised at this point or not.

If you can handle the exceptions locally you should, and it is better to handle the error as close to where it is raised as possible.

If you can't handle them locally then just having a try / finally block is perfectly reasonable - assuming there's some code you need to execute regardless of whether the method succeeded or not. For example (from Neil's comment), opening a stream and then passing that stream to an inner method to be loaded is an excellent example of when you'd need try { } finally { }, using the finally clause to ensure that the stream is closed regardless of the success or failure of the read.

However, you will still need an exception handler somewhere in your code - unless you want your application to crash completely of course. It depends on the architecture of your application exactly where that handler is.

Answer from ChrisF on Stack Exchange
Top answer
1 of 9
182

It depends on whether you can deal with the exceptions that can be raised at this point or not.

If you can handle the exceptions locally you should, and it is better to handle the error as close to where it is raised as possible.

If you can't handle them locally then just having a try / finally block is perfectly reasonable - assuming there's some code you need to execute regardless of whether the method succeeded or not. For example (from Neil's comment), opening a stream and then passing that stream to an inner method to be loaded is an excellent example of when you'd need try { } finally { }, using the finally clause to ensure that the stream is closed regardless of the success or failure of the read.

However, you will still need an exception handler somewhere in your code - unless you want your application to crash completely of course. It depends on the architecture of your application exactly where that handler is.

2 of 9
40

The finally block is used for code that must always run, whether an error condition (exception) occurred or not.

The code in the finally block is run after the try block completes and, if a caught exception occurred, after the corresponding catch block completes. It is always run, even if an uncaught exception occurred in the try or catch block.

The finally block is typically used for closing files, network connections, etc. that were opened in the try block. The reason is that the file or network connection must be closed, whether the operation using that file or network connection succeeded or whether it failed.

Care should be taken in the finally block to ensure that it does not itself throw an exception. For example, be doubly sure to check all variables for null, etc.

Discussions

Java Try Catch Finally blocks without Catch - Stack Overflow
The catch blocks and the finally are orthogonal parts of the try block. You can have either or both. With Java 7, you'll be able to have neither! ... Don't you try it with that program? It'll goto finally block and executing the finally block, but, the exception won't be handled. More on stackoverflow.com
🌐 stackoverflow.com
Need help solving 'try' without 'catch', 'finally' or resource declarations error.
OK, it took me a bit to unravel your code because either you've done a great job of obfuscating your own indentation, or codepen absolutely demolished it. So the issue is where you've done try {/ serverSocket = new ServerSocket(1098,500); This try block exists, but it has no catch or finally. Try blocks always have to do one of three things, catch an exception, terminate with a finally (This is generally to close resources like database connections, or run some code that NEEDS to be executed regardless of if an error occurs), or be a try-with-resources block (This is the Java 7+ way of closing resources, like file readers). This block currently doesn't do any of those things. So I would question then is it actually a needed try block? If so, you need to complete it. If not, you need to remove it. More on reddit.com
🌐 r/javahelp
3
3
May 5, 2022
Is there a "try" without "catch" in Javascript?
In JavaScript, the try block must be accompanied with either a catch block or finally block. From ES2019 and onward, you can have an empty catch block. It sounds like you want to have nested try-catch blocks to execute backup plans. Can you share details of the scope of your program? My gut is telling me there is most likely a cleaner way of implementing your functionality without nested try-catch blocks. More on reddit.com
🌐 r/learnjavascript
33
26
April 29, 2024
java - Why write Try-With-Resources without Catch or Finally? - Stack Overflow
Why write Try without a Catch or Finally as in the following example? protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOExc... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Scala Users
users.scala-lang.org › question
Using `try`/`finally` without `catch` - Question - Scala Users
June 22, 2020 - If I need to compute something but do some side effect before returning that value, is the correct idiom try/finally without catch ? e.g., def calculateSemesterAverage() = { import scala.io.Source def csv = Source.fromFile("some-file-to-parse.csv") try { for {line
🌐
DEV Community
dev.to › werliton › why-i-use-try-with-finally-without-catch-and-why-this-isnt-weird-code-2j92
Why I Use try with finally Without catch (and Why This Isn’t “Weird Code”) - DEV Community
2 weeks ago - If something must be undone, use finally. If something must be handled, use catch. Sometimes you need both. Sometimes only one. This pattern: try { // main logic } finally { // mandatory cleanup } “try something, but always restore state ...
🌐
Medium
medium.com › @AlexanderObregon › how-javas-try-finally-blocks-work-without-catch-96e93be92ae5
How Java’s try-finally Blocks Work Without catch | Medium
March 4, 2025 - The try block runs as expected, then the finally block runs before control moves to the next statement after try-finally. Exception Occurs, But No catch Block When an exception is thrown, Java first checks if there is a catch block to handle it.
🌐
Esdiscuss
esdiscuss.org › topic › try-without-catch-or-finally
try without catch or finally
It could even be immediately followed by a finally, which I think yields some terse, but useful syntax that's intuitive and consistent. try foo(); catch bar(); finally cleanUp(); in the same spirit as if (foo) doFoo(); else doBar(); -Michael A. Smith On Wed, Apr 18, 2012 at 5:45 AM, Jussi ...
🌐
Reddit
reddit.com › r/javahelp › need help solving 'try' without 'catch', 'finally' or resource declarations error.
r/javahelp on Reddit: Need help solving 'try' without 'catch', 'finally' or resource declarations error.
May 5, 2022 -

I keep receiving this error: 'try' without 'catch', 'finally' or resource declarations. I’ve tried to add and remove curly brackets, add final blocks, and catch blocks and nothing is working.

https://codepen.io/angelineb/pen/KKQpbqV

Top answer
1 of 3
2
OK, it took me a bit to unravel your code because either you've done a great job of obfuscating your own indentation, or codepen absolutely demolished it. So the issue is where you've done try {/ serverSocket = new ServerSocket(1098,500); This try block exists, but it has no catch or finally. Try blocks always have to do one of three things, catch an exception, terminate with a finally (This is generally to close resources like database connections, or run some code that NEEDS to be executed regardless of if an error occurs), or be a try-with-resources block (This is the Java 7+ way of closing resources, like file readers). This block currently doesn't do any of those things. So I would question then is it actually a needed try block? If so, you need to complete it. If not, you need to remove it.
2 of 3
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
Find elsewhere
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › exceptions › finally.html
The finally Block (The Java™ Tutorials > Essential Java Classes > Exceptions)
But finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated. Note: The finally block may not execute if the JVM exits while the try or catch ...
🌐
PHP
php.net › manual › en › language.exceptions.php
PHP: Exceptions - Manual
If an exception is thrown and its current function scope has no catch block, the exception will "bubble up" the call stack to the calling function until it finds a matching catch block.
🌐
Java2Blog
java2blog.com › home › core java › exception handling › can we have try without catch block in java
Can We Have Try without Catch Block in Java - Java2Blog
February 19, 2024 - In Java, it’s possible to use a try block without a catch block, but it must be followed either by a finally block or be part of a try-with-resources statement.
🌐
Microsoft Learn
learn.microsoft.com › en-us › archive › msdn-technet-forums › bf49a542-fa3b-4589-b80d-0b8754329838
Try without Catch but with finally doesn't throw error. Why no syntax error? | Microsoft Learn
You use try-finally block without catch when you want your application to guarantee execution of cleanup code (in finally block) when execution of a block of code.
🌐
TutorialsPoint
tutorialspoint.com › article › can-we-have-a-try-block-without-a-catch-block-in-java
Can we have a try block without a catch block in Java?
November 21, 2023 - A final block will always execute even though the method has a return type and try block returns some value. public class TryWithFinally { public static int method() { try { System.out.println("Try Block with return type"); return 10; } finally ...
Top answer
1 of 2
90

As explained above this is a feature in Java 7 and beyond. try with resources allows to skip writing the finally and closes all the resources being used in try-block itself. As stated in Docs

Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource.

See this code example

static String readFirstLineFromFile(String path) throws IOException {
    try (BufferedReader br = new BufferedReader(new FileReader(path))) {
        return br.readLine();
    }
}

In this example the resource is BufferReader object as the class implements the interface java.lang.AutoCloseable and it will be closed whether the try block executes successfully or not which means that you won't have to write br.close() explicitly.

Another important thing to notice here is that if you are writing the finally block yourself and both your try and finally block throw exception then the exception from try block is supressed.

While on the other hand if you are using try-with-resources statement and exception is thrown by both try block and try-with-resources statement then in this case the exception from try-with-resources statement is suppressed.

As the @Aaron has answered already above I just tried to explain you. Hope it helps.

Source: http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

2 of 2
13

This is a new feature in Java 7 and beyond. Without this, you'd need a finally block which closes the resource PrintWriter out. So the code above is equivalent to:

PrintWriter out = null;
try {
    PrintWriter out = ...
} finally {
    if(null != out) {
        try {
            out.close();
        } catch(Exception e) {} // silently ignore!
    }
}

See The try-with-resources Statement

🌐
Quora
quora.com › Can-try-be-used-without-catch
Can try be used without catch? - Quora
Allowed: try must be followed by either catch or finally (or both). So try without catch is valid only if followed by finally.
🌐
BeginnersBook
beginnersbook.com › 2013 › 04 › java-finally-block
Java Finally block – Exception handling
September 11, 2022 - class Example { public static void ...intln("Out of try-catch-finally"); } } ... 1. A finally block must be associated with a try block, you cannot use finally without a try block....
🌐
Medium
medium.com › @sureshkumar_95502 › try-without-catch-java-f4401eaaafa1
try without catch Java. In Java Exception handling, is try… | by Suresh Kumar | Medium
December 13, 2024 - public class TryFinallyExample { public static void main(String[] args) { try { invokeMethod(); } catch (Exception e) { // Handle the exception here System.out.println("Exception caught in main: " + e.getMessage()); } } // This method uses try with finally but no catch block public static void invokeMethod() throws Exception { try { // Some statements here System.out.println("Inside try block."); // throw an Exception throw new Exception("An error occurred in invokeMethod."); } finally { // The finally block is always executed System.out.println("Finally block executed."); } } }
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Statements › try...catch
try...catch - JavaScript - MDN Web Docs - Mozilla
March 9, 2026 - If you don't need the exception value, you can omit it along with the enclosing parentheses. ... The finally block contains statements to execute after the try block and catch block(s) execute, but before the statements following the try...catch...finally block.