Backend Development 7 min read

Understanding Java Exceptions: throw vs throws, final/finally/finalize, try‑catch‑finally behavior, and common exception classes

This article explains the differences between throw and throws, the roles of final, finally, and finalize, when catch can be omitted, how finally executes even after a return in catch, and lists common Java exception classes with illustrative code examples.

Java Captain
Java Captain
Java Captain
Understanding Java Exceptions: throw vs throws, final/finally/finalize, try‑catch‑finally behavior, and common exception classes

In Java, the throws keyword declares the exceptions a method may propagate, while throw actually raises a specific exception instance.

The final modifier can be applied to classes, methods, or variables to prevent inheritance, overriding, or reassignment; finally is a block that always executes after a try - catch sequence, typically for resource cleanup; finalize is a method of Object invoked by the garbage collector.

In a try‑catch‑finally construct, the catch clause may be omitted when only unchecked (runtime) exceptions are expected, because the compiler does not require handling of those.

If a return statement appears in a catch block, the finally block still executes before the method returns.

Example 1 demonstrates that a return in catch yields the value set before finally modifies the variable, resulting in an output of 30.

/*
 * java面试题--如果catch里面有return语句,finally里面的代码还会执行吗?
 */
public class FinallyDemo2 {
    public static void main(String[] args) {
        System.out.println(getInt());
    }

    public static int getInt() {
        int a = 10;
        try {
            System.out.println(a / 0);
            a = 20;
        } catch (ArithmeticException e) {
            a = 30;
            return a;
            /*
             * return a 在程序执行到这一步的时候,这里不是return a 而是 return 30;这个返回路径就形成了
             * 但是呢,它发现后面还有finally,所以继续执行finally的内容,a=40
             * 再次回到以前的路径,继续走return 30,形成返回路径之后,这里的a就不是a变量了,而是常量30
             */
        } finally {
            a = 40;
        }
        //      return a;
    }
}

Example 2 adds a return inside finally , which overrides the previous return and produces an output of 40.

package com.java_02;

/*
 * java面试题--如果catch里面有return语句,finally里面的代码还会执行吗?
 */
public class FinallyDemo2 {
    public static void main(String[] args) {
        System.out.println(getInt());
    }

    public static int getInt() {
        int a = 10;
        try {
            System.out.println(a / 0);
            a = 20;
        } catch (ArithmeticException e) {
            a = 30;
            return a;
            /*
             * return a 在程序执行到这一步的时候,这里不是return a 而是 return 30;这个返回路径就形成了
             * 但是呢,它发现后面还有finally,所以继续执行finally的内容,a=40
             * 再次回到以前的路径,继续走return 30,形成返回路径之后,这里的a就不是a变量了,而是常量30
             */
        } finally {
            a = 40;
            return a; //如果这样,就又重新形成了一条返回路径,由于只能通过1个return返回,所以这里直接返回40
        }
        //      return a;
    }
}

A non‑exhaustive list of common Java exception classes is provided, including NullPointerException , SQLException , IndexOutOfBoundsException , NumberFormatException , FileNotFoundException , IOException , ClassCastException , ArrayStoreException , IllegalArgumentException , ArithmeticException , NegativeArraySizeException , NoSuchMethodException , SecurityException , UnsupportedOperationException , and the generic RuntimeException .

BackendJavaException HandlingProgrammingfinallytry/catchthrow
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.