🚀 Day 7 – Exception Handling: More Than Just try-catch Today I focused on how exception handling should be used in real applications—not just syntax. try { int result = 10 / 0; } catch (Exception e) { System.out.println("Error occurred"); } This works… but is it the right approach? 🤔 👉 Catching generic "Exception" is usually a bad practice 💡 Better approach: ✔ Catch specific exceptions (like "ArithmeticException") ✔ Helps in debugging and handling issues more precisely ⚠️ Another insight: Avoid using exceptions for normal flow control Example: if (value != null) { value.process(); } 👉 is better than relying on exceptions 💡 Key takeaway: - Exceptions are for unexpected scenarios, not regular logic - Proper handling improves readability, debugging, and reliability Small changes here can make a big difference in production code. #Java #BackendDevelopment #ExceptionHandling #CleanCode #LearningInPublic
Java Exception Handling Best Practices
More Relevant Posts
-
⏰ Temporary Field: When instance variables only show up for certain operations, cluttering your class interface with null-initialized fields. Here's how Extract Class refactoring transforms your code: ✅ Move temporary fields into dedicated classes ✅ Eliminate null-initialized field clutter ✅ Create clearer class interfaces ✅ Make object state predictable at every stage 🎯 Key takeaway: Extract Class refactoring organizes temporary state into focused objects that only exist when they're needed. What's your experience with refactoring Temporary Fields? Share your favorite techniques in the comments. #CleanCode #Refactoring #Java #TemporaryField #DeveloperTips https://lnkd.in/gJQeWPJ9
To view or add a comment, sign in
-
-
Some of the hardest problems become manageable once you recognize a repeating pattern. 🚀 Day 105/365 — DSA Challenge Solved: Subarrays with K Different Integers Problem idea: We need to count subarrays that contain exactly k distinct integers. Efficient approach: Use the powerful trick: subarrays with exactly k distinct = subarrays with ≤ k distinct − subarrays with ≤ (k − 1) distinct Steps: 1. Use a sliding window with a hashmap to track frequency of elements 2. Expand window by moving right pointer 3. If distinct count exceeds k, shrink window from the left 4. Count valid subarrays ending at each index 5. Subtract results to get exact count This pattern converts a hard problem into a manageable one. ⏱ Time: O(n) 📦 Space: O(n) Day 105/365 complete. 💻 260 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #SlidingWindow #HashMap #LeetCode #LearningInPublic
To view or add a comment, sign in
-
-
Have you ever debugged a production issue where logs show the same error everywhere… but you still can’t figure out what actually failed? I used to make this mistake a lot — catch exception → log error → throw again Service logs Repository logs Controller logs Same exception 3–4 times. Just noise. Then I changed one simple thing: don’t log where you’re just throwing the exception. Now I follow this: Service/Repository → just throw or wrap the exception Log only once at the boundary (API / consumer) Always add context We often don’t pay much attention to logging, but it plays a crucial role when debugging a system. You need clarity on where to log, what to log, and where not to. Now instead of messy logs, I get one clear error log with full context. Debugging feels less like guessing and more like tracing a story. Less logs. Better logs. That’s the real strategy. #BackendDevelopment #Microservices #SystemDesign #Java #SpringBoot #Logging #SoftwareEngineering #CleanCode
To view or add a comment, sign in
-
Day 73/100 Logging (Very Important) 📝 Today I focused on something that often gets ignored but is critical in real-world applications — logging. Learned: Why logging is important for debugging and monitoring applications Different log levels and when to use them (INFO, WARN, ERROR) Key takeaway: Logs are not just for debugging, they help understand what’s happening inside the application without stopping it. Starting to realize that good logging is a must for building reliable backend systems. #BackendDevelopment #Java #Logging #LearningInPublic #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 16 of #100DaysOfCode Today’s problem: Minimum Absolute Difference 📊 🔍 Problem Understanding: Given an array of distinct integers, find all pairs with the minimum absolute difference. 🧠 Approach: First, I sorted the array Then compared adjacent elements (since minimum difference will always be between neighbors in sorted order) Tracked the minimum difference and stored all valid pairs 👉 Key Insight: Sorting reduces unnecessary comparisons and simplifies the problem ⚙️ Concepts Used: Sorting (Arrays.sort) Greedy observation (check only neighbors) List handling for storing pairs 📊 Complexity: ⏱️ Time Complexity: O(N log N) (due to sorting) 💾 Space Complexity: O(N) (for storing result pairs) 📚 Key Learnings: Learned how sorting simplifies problems Understood how to reduce complexity from brute force O(N²) → O(N log N) Improved thinking in terms of patterns and optimization 💯 Result: ✔️ Accepted (All test cases passed) ✔️ Runtime: 1 ms (100%) 🔥 ✔️ Clean and efficient solution Sometimes the best optimization is just sorting + observation 💡 Let’s keep building consistency 🚀 #Day16 #100DaysOfCode #Java #DSA #LeetCode #ProblemSolving #Sorting
To view or add a comment, sign in
-
-
Traditional Loops vs Streams in Java When working with collections, developers often face this choice 👇 Traditional Loops - Imperative approach (how to do it) - Step-by-step control - More verbose and manual Streams - Declarative approach (what to do) - Functional style (filter, map, collect) - Cleaner and more expressive code Key Insight Streams shift the focus from iteration to transformation, making code easier to read and maintain. * When to use what? - Use loops when you need fine-grained control - Use streams for cleaner, pipeline-based data processing There’s no one-size-fits-all — choose based on readability, performance, and use case. #Java #CleanCode #Streams #SoftwareEngineering #BackendDevelopment
To view or add a comment, sign in
-
-
🚀 Day 21 of #128DaysOfCode 🔍 Key Learnings: Efficient searching in O(log n) Using low & high to narrow the range Finding correct position even if target is absent 🧠 Approach: Compare mid with target → move left/right → return index or insert position Consistency is key 🔥 #DSA #Java #CodingJourney #PlacementPreparation
To view or add a comment, sign in
-
-
Day 78 - Binary Tree Level Order Traversal Implemented level-by-level traversal of a binary tree using a queue (BFS). Approach: • Use a queue to process nodes • Traverse level by level • Track size of each level • Store values accordingly Time Complexity: O(n) Space Complexity: O(n) #Day78 #LeetCode #BinaryTree #DSA #Java #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
Day 42 of consistency 💻🔥 Solved Add Two Numbers using linked lists — a classic problem that really tests understanding of pointers, carry handling, and edge cases. Key takeaways: Handling carry efficiently is crucial Dummy nodes make list problems much cleaner Iterative approach keeps space optimal Not the fastest runtime yet, but improvement is a process — optimizing step by step. Consistency > Perfection. #Day42 #LeetCode #DSA #Java #CodingJourney #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
#Post11 In the previous post(https://lnkd.in/dynAvNrN), we saw how to create threads in Java. Now let’s talk about a problem. If creating threads is so simple… why don’t we just create a new thread every time we need one? Let’s say we are building a backend system. For every incoming request/task, we create a new thread: new Thread(() -> { // process request }).start(); This looks simple. But this approach breaks very quickly in real systems because of below mentioned problems. Problem 1: Thread creation is expensive Creating a thread is not just creating an object. It involves: • Allocating memory (stack) • Registering with OS • Scheduling overhead Creating thousands of threads = performance degradation Problem 2: Too many threads → too much context switching We already saw this earlier(https://lnkd.in/dYG3v-vb). More threads does NOT mean more performance. Instead: • CPU spends more time switching • Less time doing actual work Problem 3: No control over thread lifecycle When you create threads manually: • No limit on number of threads • No reuse • Hard to manage failures This quickly becomes difficult to manage as the system grows. So what’s the solution? Instead of creating threads manually: we use something called the Executor Framework. In simple words consider the framework to be like: Earlier, we were manually hiring a worker (thread) for every task. With Executor, we have a team of workers (thread pool), and we just assign tasks to them. Key idea Instead of: Creating a new thread for every task We do: Submit tasks to a pool of reusable threads This is exactly what Java provides using: Executor Framework Key takeaway Manual thread creation works for learning, but does not scale in real-world systems. Thread pools help: • Control number of threads • Reduce overhead • Improve performance We no longer manage threads directly — we delegate that responsibility to the Executor Framework. In the next post, we’ll see how Executor Framework works and how to use it in Java. #Java #Multithreading #Concurrency #BackendDevelopment #SoftwareEngineering
To view or add a comment, sign in
Explore related topics
- Tips for Exception Handling in Software Development
- Best Practices for Exception Handling
- Debugging Tips for Software Engineers
- Coding Techniques for Flexible Debugging
- Ways to Improve Coding Logic for Free
- Coding Best Practices to Reduce Developer Mistakes
- Advanced Debugging Techniques for Senior Developers
- Best Practices for Debugging Code
- Best Practices for Writing Clean Code
- Simple Ways To Improve Code Quality
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development