Java Exception Handling
Exception:– An Exception is an unexpected error that occurs during the run-time of a program.
Error vs Exception: What is the difference?
When a program encounters an error, it means there is a significant issue that a well-designed program should not try to fix. On the other hand, an exception indicates a particular situation that a well-designed program should try to handle.
Types of Exceptions:-
Checked Exception:- This mainly includes IO Exceptions and Compile time Exceptions
Unchecked Exceptions:– This covers both Runtime Exceptions and Null Pointer Exceptions.
Handle Exceptions
try block: The try block comprises a set of statements that may throw an exception.
Catch Block: When there is an uncertain condition in the try block, the catch block comes in handy to handle it. It is mandatory to have a catch block right after the try block to handle any exceptions that may be thrown by the try block.
Finally Block: When programming in Java, the finally block is a section of code that will always run, regardless of whether or not an exception has been caught. If there is a catch block following a try block, it will be executed before the finally block. However, if there is no catch block, the finally block will be executed immediately after the try block.
Example:-
try {
// block of code to monitor for errors
// the code you think can raise an exception
} catch (ExceptionType1 exOb) {
// exception handler for ExceptionType1
} catch (ExceptionType2 exOb) {
// exception handler for ExceptionType2
}
// optional
finally { // block of code to be executed after try block ends
}
final vs finally vs finalize
Below is a table that outlines the distinctions between the terms final, finally, and finalize:
final
- A final keyword can be used with classes, methods, and variables.
- A final class cannot be inherited.
- A final method cannot be overridden.
- A final variable cannot be reassigned.
finally
- A finally block is always executed, regardless of whether an exception is thrown or not.
- The finally block is executed after the try block and catch block, but before the program control returns to the calling method.
- The finally block can be used to close resources, such as files and database connections.
finalize
- The finalize() method is called by the garbage collector when an object is no longer needed.
- The finalize() method can be used to perform cleanup operations, such as releasing resources or writing data to a file.
- The finalize() method is not guaranteed to be called, so it should not be used to perform critical operations.
throw keyword:- When using Java, the throw keyword is utilized to throw an exception from a block of code or a method. It is possible to throw either a checked or unchecked exception. The throw keyword is frequently used for throwing custom exceptions.
throws keyword:- If a try/catch block is not present, the throws keyword can be used to manage exceptions. This keyword specifies the specific exceptions that a method should throw if an exception happens.
Java Cheat Sheet
Java is a programming language and platform that has been widely used since its development by James Gosling in 1982. It follows the Object-oriented Programming concept and can run programs written in any programming language. Java is a high-level, object-oriented, secure, robust, platform-independent, multithreaded, and portable programming language All those words are collectively called Java Buzzwords. It is commonly used for programming web-based, window, enterprise, and mobile applications. This Java Cheat Sheet article has been written by experts in Java and based on the experience of students who have recently undergone Java interviews.
This Core Java Cheat Sheet has been designed by Java experts, based on the experience of students who have recently undergone Java interviews. Whether you are a beginner or an experienced Java developer, this Java Cheat Sheet is a valuable resource for quickly accessing essential syntax, concepts, and best practices related to Java Programming.