Wednesday, 4 June 2025

Exception Handling

 Exception handling is a critical component of robust application development in Spring Boot. Properly managed exceptions ensure that your application can handle unexpected scenarios gracefully, maintain stability, and provide meaningful feedback to the user.

Why Exception Handling is Important

  • Robustness: Exception handling ensures that your application can recover from unexpected situations.
  • User Experience: Proper error messages help users understand what went wrong.
  • Debugging and Maintenance: Well-handled exceptions make it easier to debug and maintain the application.
  • Compliance and Security: Proper handling prevents sensitive error details from being exposed to the user.
  • Default Spring Boot Exception Handling

    Spring Boot provides a default global error handler, which handles common exceptions and returns a standard JSON response with details like status code, error message, etc.


Controller Level Exception Handling:











Global Exception Handling
Reference link:

https://www.devskillbuilder.com/exception-handling-in-spring-boot-f8d13eace4ba













Monday, 26 May 2025

Java 8 Interview question with solution

 Example:1 / Filter employees with salary > 50000 and get their names(map example)

Solution:

Output:

High Earning Employees: [Alice, Charlie]   

Program:

import java.util.*;

import java.util.stream.Collectors;

class Employee {

    String name;

    double salary;

    Employee(String name, double salary) {

        this.name = name;

        this.salary = salary;

    }

}

public class EmployeeMapExample {

    public static void main(String[] args) {

        List<Employee> employees = Arrays.asList(

                new Employee("Alice", 60000),

                new Employee("Bob", 45000),

                new Employee("Charlie", 70000)

        );


        // Filter employees with salary > 50000 and get their names

        List<String> highEarnerNames = employees.stream()

                .filter(emp -> emp.salary > 50000) // condition

                .map(emp -> emp.name)             // transform Employee to name

                .collect(Collectors.toList());    // collect as List<String>


        System.out.println("High Earning Employees: " + highEarnerNames);

    }

}