Skip to content

This is a tutorial project. JavaBasics_Task_174_V0.1 Demonstrating the guaranteed execution of the finally block without a catch block during a fatal error. 250226_1148

License

Notifications You must be signed in to change notification settings

YuriiJavaDev/JavaBasics_Task_174_V0.1

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Critical Resource Manager (JavaBasics_Task_174_V0.1)

πŸ“– Description

This project illustrates the behavior of the finally block when an exception occurs but is not caught by a catch block. It proves that the Java Virtual Machine guarantees the execution of the finally block before the program terminates due to a fatal error, which is essential for resource cleanup.

πŸ“‹ Requirements Compliance

  • Task: Intentionally cause a division by zero error in the try block.
  • Constraint: Do not use a catch block.
  • Mechanism: Implement a finally block to display a sensor message.
  • Goal: Observe that the finally message is printed before the program crashes with an exception.

πŸš€ Architectural Stack

  • Java 8+

πŸ—οΈ Implementation Details

The try-finally structure is used when we want to ensure specific code runs regardless of success or failure, but we want the exception to propagate further (to the caller or the JVM). In this case, the ArithmeticException is thrown, the finally block executes its print statement, and then the JVM terminates the thread with the error log.

πŸ“‹ Expected result

=====================================================================
Sensor reports: finally execution completed. Even in case of a crash.
=====================================================================

The following message comes from the JVM, which terminates the thread
and writes an error to the error log:

Exception in thread "main" java.lang.ArithmeticException: / by zero
	at com.yurii.pavlenko.Solution.main(Solution.java:10)

Process finished with exit code 1

πŸ’» Code Example

Project Structure:

src/com/yurii/pavlenko/
                └── Solution.java

Code

package com.yurii.pavlenko;

public class Solution {
    public static void main(String[] args) {
        try {
            int result = 5 / 0;
            System.out.println("Result: " + result);

        } finally {
            System.out.println("=====================================================================");
            System.out.println("Sensor reports: finally execution completed. Even in case of a crash.");
            System.out.println("=====================================================================");
            System.out.println();
            System.out.println("The following message comes from the JVM, which terminates the thread\n" +
                               "and writes an error to the error log:");
            System.out.println();
        }
    }
}

βš–οΈ License

This project is licensed under the MIT License.

Copyright (c) 2026 Yurii Pavlenko

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files...

License: MIT

About

This is a tutorial project. JavaBasics_Task_174_V0.1 Demonstrating the guaranteed execution of the finally block without a catch block during a fatal error. 250226_1148

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages