Skip to main content

Posts

Showing posts with the label Exceptions and Error Handling

Socket (TCP & UDP) communication in Java

Socket communication in Java enables communication between two endpoints over a network. There are two main types of sockets: TCP sockets and UDP sockets. Let's explain both types with examples: TCP Socket Communication: 1. **Server Side**:    - The server creates a `ServerSocket` object to listen for incoming connections on a specific port.    - When a client connects, the server accepts the connection and creates a `Socket` object to communicate with the client.    - The server reads from and writes to the socket's input and output streams to communicate with the client. import java.io.*; import java.net.*; public class TCPServer {     public static void main(String[] args) throws IOException {         ServerSocket serverSocket = new ServerSocket(12345);         System.out.println("Server started. Waiting for client...");         Socket clientSocket = serverSocket.accept();         System.out.println("Client connected.");         BufferedReader in = new Bu

JUnit Testing Framework

 JUnit is a popular testing framework for Java programming language. It provides a simple and efficient way to write unit tests for your Java code, allowing you to ensure that your code functions as expected and continues to work correctly as you make changes. Key Features of JUnit: 1. Annotations:  JUnit uses annotations to identify methods that specify test cases and setup/teardown operations. Annotations include `@Test`, `@Before`, `@After`, `@BeforeClass`, and `@AfterClass`. 2. Assertions:  JUnit provides a set of assertion methods such as `assertEquals`, `assertTrue`, `assertFalse`, `assertNotNull`, etc., to validate expected outcomes of tests. 3. Test Runners:  JUnit includes various test runners that execute test cases and report results. The default runner is `BlockJUnit4ClassRunner`, but JUnit also supports parameterized tests and suites. 4. Parameterized Tests:  JUnit allows you to write parameterized tests using the `@ParameterizedTest` annotation, which enables you to run t

Custom Exceptions

In Java, you can create custom exceptions by extending the `Exception` class or one of its subclasses like `RuntimeException`. Custom exceptions are useful when you want to define your own exception types to represent specific error conditions in your application. Syntax for creating a custom exception: public class CustomException extends Exception {     // Constructors, methods, and additional members can be defined here } Example of a custom exception: public class InvalidAgeException extends Exception {     public InvalidAgeException(String message) {         super(message);     } } public class AgeValidator {     public static void validateAge(int age) throws InvalidAgeException {         if (age < 0) {             throw new InvalidAgeException("Age cannot be negative");         }         if (age < 18) {             throw new InvalidAgeException("Must be at least 18 years old");         }         System.out.println("Age is valid");     }     pub

Chained Exceptions

 Chained exceptions, also known as nested exceptions, allow you to associate one exception with another. This feature is useful when you want to provide more context or information about the cause of an exception. In Java, you can chain exceptions using constructors that take a `Throwable` (or its subclasses) as an argument. Syntax: try {     // Code that may throw an exception } catch (ExceptionType1 e1) {     throw new ExceptionType2("Additional information", e1); } Explanation: - Inside a `catch` block, you can create a new exception object and pass the original exception (`e1`) as the cause. - The chained exception (`ExceptionType2`) includes a message and the original exception (`e1`) as its cause. - This technique allows you to preserve the original exception's stack trace and context while providing additional information about the higher-level exception. - Chained exceptions can be caught and processed at higher levels of the call stack, allowing for better error

Throwing Exceptions

 In Java, the `throw` statement is used to explicitly throw an exception within a method or block of code. It is typically used when an exceptional condition occurs that the method cannot handle itself, and it wants to signal to the calling code that something went wrong. Syntax: throw throwableObject; Explanation: - The `throw` statement requires an argument, which is usually an instance of a subclass of `Throwable` (either an exception or an error). - The argument passed to `throw` can be any expression that results in a throwable object. - When a `throw` statement is executed, the program flow is immediately transferred out of the current method or block and back to the calling code. - The caller can then catch the thrown exception using a `try-catch` block or propagate it further up the call stack. Example: public class AgeValidator {     public static void validateAge(int age) {         if (age < 0) {             throw new IllegalArgumentException("Age cannot be negative&q

The finally Block

 The `finally` block in Java is used to define a block of code that will be executed regardless of whether an exception is thrown or not within the `try` block. It provides a way to perform cleanup tasks such as closing resources, releasing locks, or finalizing operations. Syntax: try {     // Code that may throw an exception } catch (ExceptionType ex) {     // Code to handle the exception } finally {     // Code to be executed regardless of whether an exception occurs or not } Explanation: - The `finally` block is optional and can appear after the `try` block or after the last `catch` block. - The code inside the `finally` block will always be executed, irrespective of whether an exception occurs in the `try` block or whether it's caught by a `catch` block. - The `finally` block is often used to release resources that were acquired in the `try` block, ensuring they are properly released even if an exception occurs. - If an exception occurs in the `try` block and is caught by a `ca

The catch Blocks

 The `catch` blocks in Java are used to handle exceptions that occur within a `try` block. Each `catch` block specifies the type of exception it can handle, allowing you to provide different handling logic for different types of exceptions. Syntax: try {     // Code that may throw an exception } catch (ExceptionType1 ex1) {     // Code to handle ExceptionType1 } catch (ExceptionType2 ex2) {     // Code to handle ExceptionType2 } finally {     // Cleanup code (optional) } ``` Explanation: - Each `catch` block specifies the type of exception it can handle, denoted by `ExceptionType`. If an exception of that type (or a subtype) occurs in the `try` block, the corresponding `catch` block is executed. - You can have multiple `catch` blocks to handle different types of exceptions. - The order of `catch` blocks is important, as Java checks them sequentially from top to bottom. It's recommended to catch subclasses before their superclasses to avoid unreachable code warnings. - If an excepti

The `try` block in Java

The `try` block in Java is used to enclose the code that may throw an exception. It is followed by one or more `catch` blocks and an optional `finally` block. The `try` block allows you to handle exceptions gracefully by providing a mechanism to catch and handle any exceptions that occur during the execution of the enclosed code. Syntax: try {     // Code that may throw an exception } catch (ExceptionType1 ex1) {     // Code to handle ExceptionType1 } catch (ExceptionType2 ex2) {     // Code to handle ExceptionType2 } finally {     // Cleanup code (optional) } Explanation: - The `try` block encloses the code that you want to monitor for exceptions. - If an exception occurs within the `try` block, the control is transferred to the appropriate `catch` block that matches the type of the thrown exception. - You can have multiple `catch` blocks to handle different types of exceptions. - The `finally` block, if present, is executed regardless of whether an exception occurred or not. It is co

Catching and Handling Exceptions

Catching and handling exceptions is a crucial aspect of Java programming, as it allows developers to gracefully manage unexpected errors and prevent program termination. This is typically achieved using `try`, `catch`, and `finally` blocks. Let's explore each of these blocks and how they are used to catch and handle exceptions: 1. `try` Block: The `try` block contains the code that may potentially throw an exception. It is followed by one or more `catch` blocks to handle specific types of exceptions, or by a `finally` block for cleanup code. 2. `catch` Block: A `catch` block is used to catch and handle exceptions that are thrown within the corresponding `try` block. It specifies the type of exception it can handle and provides code to handle the exception. 3. `finally` Block: The `finally` block is optional and is used to execute cleanup code that should be run whether an exception occurs or not. This block is commonly used to release resources such as file handles or database conn

Exceptions and Errors

Exceptions: Exceptions represent exceptional conditions that can occur during the execution of a program. They can be caused by various factors such as invalid input, network issues, or file not found. Exceptions are further categorized into two types: 1. Checked Exceptions: These are exceptions that are checked at compile time. They are typically recoverable and should be handled by the programmer. Examples include `IOException`, `SQLException`, etc. 2. Unchecked Exceptions (Runtime Exceptions): These are exceptions that are not checked at compile time. They usually indicate programming errors or unexpected conditions. Examples include `NullPointerException`, `ArrayIndexOutOfBoundsException`, etc. Errors: Errors, on the other hand, represent serious problems that are typically beyond the control of the program. They are usually caused by system-level issues or conditions such as out of memory, stack overflow, or hardware failures. Errors are not normally caught or handled by the pro