🚀 Day 21 | Core Java Learning Journey 📌 Topic: Exception Handling Keywords in Java Today I learned the most important Java exception handling keywords: try, catch, finally, throw, throws — and the commonly confused finalize(). Understanding them is essential for writing clean, production-ready Java code 💡 🔹 try ✔ Wraps risky code ✔ Must be followed by catch or finally ✔ Cannot exist alone ✔ Multiple catch blocks allowed Syntax : try { // risky code } catch(Exception e) { // handling } 🔹 catch ✔ Handles exceptions from try ✔ Takes exception object as parameter ✔ Order matters (Child → Parent) ✔ Multiple catch blocks allowed 🔹 finally ✔ Always executes (whether exception occurs or not) ✔ Executes even if return statement is present ✔ Used for cleanup (files, DB, etc.) ✔ Won’t execute only if JVM crashes or System.exit() is called 🔹 throw ✔ Explicitly throws an exception ✔ Used inside method body ✔ Can throw checked & unchecked exceptions ✔ Followed by exception object throw new IllegalArgumentException("Invalid Age"); 🔹 throws ✔ Declares exception responsibility ✔ Used in method signature ✔ Mainly for checked exceptions ✔ Can declare multiple exceptions public void readFile() throws IOException { // code } 🔥 throw vs throws ✔ throw → Inside method body ✔ throws → In method declaration ✔ throw → Single exception ✔ throws → Multiple exceptions 🔹 finalize() (Important) ✔ NOT part of exception handling ✔ Belongs to Garbage Collection ✔ Defined in Object class ✔ Called before object destruction ✔ Deprecated (Java 9+) 🔥 finally vs finalize() ✔ finally → Exception handling block ✔ finalize() → GC method ✔ finally → (Almost) always runs ✔ finalize() → May or may not run 📌 Key Takeaways ✔ finally ensures cleanup ✔ throw creates exceptions ✔ throws delegates responsibility ✔ finalize() relates to memory management Small keywords — powerful concepts 💻🚀 Special thanks to Vaibhav Barde Sir . #CoreJava #JavaLearning #ExceptionHandling #JavaDeveloper #OOP #LearningJourney
Java Exception Handling Keywords: try, catch, finally, throw, throws
More Relevant Posts
-
Day 38 at #TapAcademy 🚀 ArrayList in Java – A Must-Know for Every Developer When working with Java, one of the most commonly used data structures is ArrayList — a powerful and flexible part of the Java Collection Framework. 🔹 What is ArrayList? ArrayList is a resizable array implementation of the List interface. Unlike traditional arrays, it can grow or shrink dynamically as elements are added or removed. 🔹 Why use ArrayList? ✔ Dynamic size (no need to define length in advance) ✔ Allows duplicate elements ✔ Maintains insertion order ✔ Provides fast access using index ✔ Comes with rich built-in methods 🔹 Common Methods: 📌 add(E e) – Add element 📌 get(int index) – Access element 📌 set(int index, E e) – Update element 📌 remove(int index) – Delete element 📌 size() – Get number of elements 🔹 Constructors: 📌 ArrayList() – Creates an empty list 📌 ArrayList(int initialCapacity) – Sets initial size 📌 ArrayList(Collection<? extends E> c) – Creates list from another collection 💡 Example: ArrayList<String> names = new ArrayList<>(); names.add("Alice"); names.add("Bob"); names.add("Charlie"); System.out.println(names); 🔹 Difference: Arrays vs ArrayList 📌 Arrays ▪ Fixed size (cannot grow/shrink) ▪ Can store primitives (int, char, etc.) ▪ No built-in methods (limited operations) ▪ Faster for basic operations 📌 ArrayList ▪ Dynamic size (resizable) ▪ Stores only objects (wrapper classes like Integer) ▪ Rich built-in methods (add, remove, etc.) ▪ More flexible and easy to use 📈 Understanding ArrayList is essential for writing efficient, clean, and scalable Java programs—whether you're preparing for interviews or building real-world applications. #Java #ArrayList #Programming #Coding #DataStructures #JavaDeveloper #Learning #Tech #TapAcademy
To view or add a comment, sign in
-
-
DAY 25: CORE JAVA 🚀 7 Most Important Elements of a Java Class While learning Java & Object-Oriented Programming (OOP), understanding the internal structure of a class is essential. A Java class mainly contains two categories of members: Class-level (static) and Object-level (instance). Here are the 7 most important elements of a Java class: 🔹 1. Static Variables (Class Variables) These variables belong to the class, not to individual objects. They are shared among all objects of the class. 🔹 2. Static Block A static block is used to initialize static variables. It runs only once when the class is loaded into memory. 🔹 3. Static Methods Static methods belong to the class and can be called without creating an object. 🔹 4. Instance Variables These variables belong to an object. Every object created from the class has its own copy. 🔹 5. Instance Block An instance block runs every time an object is created, before the constructor executes. 🔹 6. Instance Methods Instance methods operate on object data and require an object of the class to be invoked. 🔹 7. Constructors Constructors are special methods used to initialize objects when they are created. 💡 Simple Understanding: 📦 Class Level • Static Variables • Static Block • Static Methods 📦 Object Level • Instance Variables • Instance Block • Instance Methods • Constructors ⚠️ Important Rule: Static members can access only static members directly, while instance members can access both static and instance members. Understanding these 7 elements of a class helps build a strong foundation in Java and OOP concepts, which is essential for writing efficient and well-structured programming TAP Academy #Java #JavaDeveloper #OOP #Programming #Coding #SoftwareDevelopment #LearnJava
To view or add a comment, sign in
-
-
🚀 Day 30 | Core Java Learning Journey 📌 Topic: Map Hierarchy in Java Today, I explored the Map Hierarchy in Java Collections Framework — understanding how different Map interfaces and classes are structured and related. 🔹 What is Map in Java? ✔ Map is an interface that stores key-value pairs ✔ Each key is unique and maps to a specific value ✔ It is part of java.util package 🔹 Map Hierarchy (Understanding Structure) ✔ Map (Root Interface) ⬇ ✔ SortedMap (extends Map) ⬇ ✔ NavigableMap (extends SortedMap) ⬇ ✔ TreeMap (implements NavigableMap) 🔹 Important Implementing Classes ✔ HashMap • Implements Map • Does NOT maintain order • Allows one null key ✔ LinkedHashMap • Extends HashMap • Maintains insertion order ✔ TreeMap • Implements NavigableMap • Stores data in sorted order • Does NOT allow null key ✔ Hashtable • Implements Map • Thread-safe (synchronized) • Does NOT allow null key/value 🔹 Key Differences ✔ HashMap → Fast, no ordering ✔ LinkedHashMap → Maintains insertion order ✔ TreeMap → Sorted data ✔ Hashtable → Thread-safe but slower 📌 When to Use What? ✅ Use HashMap → when performance is priority ✅ Use LinkedHashMap → when insertion order matters ✅ Use TreeMap → when sorting is required ✅ Use Hashtable → when thread safety is needed 💡 Key Takeaway: Understanding Map hierarchy helps in choosing the right data structure based on use-case rather than just coding blindly. 🙏 Special thanks to Vaibhav Barde Sir for the guidance! 🔥 #CoreJava #JavaLearning #JavaDeveloper #Map #HashMap #TreeMap #LinkedHashMap #Hashtable #JavaCollections #Programming #LearningJourney
To view or add a comment, sign in
-
-
Advancing in Java Full Stack Development – Daily Training Insights Today I learned about Exception Handling in Java, which is an important concept used to handle runtime errors and maintain the normal flow of a program. Exception handling helps a program manage unexpected situations like dividing by zero, accessing invalid array indexes, or handling file errors without abruptly stopping the program. Introduction to Exception Handling: Exception handling in Java is a mechanism used to handle runtime errors so that the program can continue executing normally. An exception is an unwanted event that occurs during program execution and disrupts the normal flow of instructions. Java provides built-in support to detect and manage such errors effectively. Try Block: The try block contains the code that may generate an exception. Any statements that might cause a runtime error are written inside this block so that Java can monitor them and transfer control if an exception occurs. Catch Block: The catch block is used to handle the exception generated in the try block. When an exception occurs, the program jumps to the catch block where the error is handled properly, preventing the program from crashing. Finally Block: The finally block contains code that will always execute whether an exception occurs or not. It is mainly used for cleanup activities like closing files, database connections, or releasing resources. Throw Keyword: The throw keyword is used to explicitly create and throw an exception within the program when a specific condition occurs. Throws Keyword: The throws keyword is used in a method declaration to indicate that a method might produce an exception during execution. It informs the caller that the exception should be handled. Checked and Unchecked Exceptions: Exceptions in Java are classified into checked and unchecked exceptions. Checked exceptions are verified by the compiler and must be handled during compilation, whereas unchecked exceptions occur during runtime and are not checked at compile time. Learning Core Java step by step every day and strengthening my programming fundamentals as part of my journey toward becoming a better software developer. #Java #CoreJava #ExceptionHandling #JavaDeveloper #Programming #LearningJourney #SoftwareDevelopment #100DaysOfCode👍
To view or add a comment, sign in
-
💡 Mastering Exception Handling in Java: Try & Catch Explained While writing robust applications, handling unexpected errors gracefully is just as important as writing the main logic. That’s where try-catch blocks come into play in Java. 🔹 Why use try-catch? It helps prevent your program from crashing and allows you to handle runtime errors effectively, ensuring a smooth user experience. 🔸 1. Basic Try-Catch Block A simple try-catch block is used to handle a single exception. public class Example { public static void main(String[] args) { try { int result = 10 / 0; // ArithmeticException } catch (ArithmeticException e) { System.out.println("Cannot divide by zero!"); } } } 👉 Here, the try block contains code that may throw an exception, and the catch block handles it. 🔸 2. One Try Block with Multiple Catch Blocks Java allows multiple catch blocks to handle different types of exceptions separately. public class MultipleCatchExample { public static void main(String[] args) { try { int[] arr = new int[5]; arr[10] = 50; // ArrayIndexOutOfBoundsException int result = 10 / 0; // ArithmeticException } catch (ArithmeticException e) { System.out.println("Arithmetic Exception occurred"); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Array Index is out of bounds"); } catch (Exception e) { System.out.println("Some other exception occurred"); } } } 👉 Multiple catch blocks allow you to handle each exception type differently, making your code more precise and readable. ✨ Key Takeaways: ✔ Always handle specific exceptions before general ones ✔ Improves program stability and debugging ✔ Essential for writing production-level Java code 🚀 Keep learning, keep building, and make your code resilient! #Java #ExceptionHandling #Programming #Coding #Developers #Learning #JavaDeveloper TAP Academy
To view or add a comment, sign in
-
-
🚀 Understanding Key Java Differences: throw vs throws & final, finally, finalize Java has several keywords that sound similar but serve completely different purposes. Understanding these differences is essential for writing clean and efficient code. Let’s break them down 👇 🔹 throw vs throws 👉 throw Used to explicitly throw an exception Used inside a method or block Throws a single exception at a time throw new ArithmeticException("Error occurred"); 👉 throws Used in method signature Declares exceptions that a method might throw Can declare multiple exceptions void readFile() throws IOException, SQLException { // code } 💡 Key Difference: throw is used to actually throw an exception, while throws is used to declare exceptions. 🔹 final vs finally vs finalize 👉 final Keyword used with variables, methods, and classes Variable → value cannot be changed Method → cannot be overridden Class → cannot be inherited final int x = 10; 👉 finally Block used with try-catch Always executes (whether exception occurs or not) Used for cleanup activities try { int a = 10 / 0; } finally { System.out.println("Cleanup done"); } 👉 finalize Method called by Garbage Collector before object destruction Used for cleanup (rarely used in modern Java) protected void finalize() throws Throwable { System.out.println("Object is destroyed"); } 💡 Key Difference: final → restriction keyword finally → execution block finalize → method for cleanup before garbage collection ✨ Takeaway: Small keywords can make a big difference in Java. Mastering these improves your code quality and helps you handle exceptions and memory more effectively. Keep learning, keep coding, and keep growing 💻🚀 #Java #ExceptionHandling #ProgrammingConcepts #Developers #CodingJourney #KeepLearning #OOP TAP Academy
To view or add a comment, sign in
-
-
DAY 29: CORE JAVA 🔗 Constructor Chaining in Java using "super()" (Inheritance) While learning Java OOP concepts, one interesting topic I explored is Constructor Chaining in Inheritance. 📌 What is Constructor Chaining? Constructor chaining is the process of calling one constructor from another constructor. In inheritance, a child class constructor calls the parent class constructor using "super()". This ensures that the parent class variables are initialized before the child class variables. ⚙️ Key Points to Remember • "super()" is used to call the parent class constructor. • It must be the first statement inside the child constructor. • If we don’t explicitly write "super()", Java automatically calls the parent class default constructor. • This mechanism ensures proper initialization of objects in inheritance hierarchy. 💡 Example Scenario Parent Class: class Test1 { int x = 100; int y = 200; } Child Class: class Test2 extends Test1 { int a = 300; int b = 400; } When an object of "Test2" is created, Java first calls the parent constructor, initializes "x" and "y", and then initializes "a" and "b". 📊 Execution Flow 1️⃣ Object of child class is created 2️⃣ Child constructor calls "super()" 3️⃣ Parent constructor executes first 4️⃣ Control returns to child constructor This concept is very important for understanding object initialization, inheritance hierarchy, and memory allocation in Java. 🚀 Learning these small internal mechanisms of Java helps build a strong foundation in Object-Oriented Programming. TAP Academy #Java #OOP #ConstructorChaining #Inheritance #JavaProgramming #SoftwareDevelopment #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 19 at TAP Academy – Exploring Java Arrays in Depth Today’s session was focused on gaining a deeper understanding of Java Arrays and how they are used in real programming scenarios. We started with a quick recap of the limitations of arrays, such as storing only homogeneous data, fixed size allocation, and the requirement of contiguous memory. 💡 One of the key topics covered was the three different ways to create arrays in Java: 1️⃣ Standard array creation using new keyword (most commonly used in real programs). 2️⃣ Creating arrays with predefined values using new int[]{} syntax. 3️⃣ Shorthand array initialization using {} without the new keyword. We also explored how arrays can be created for different primitive data types like byte, short, int, float, double, char, and boolean, along with the corresponding Scanner methods used to take input from users. 🔤 Another interesting concept was handling character input in Java, where we learned the workaround using: scan.next().charAt(0) since Java does not provide a direct nextChar() method. 📦 The session then moved to Arrays of Strings, which highlighted how Java treats Strings as objects and how they can be stored and accessed in arrays similar to primitive types. 👨💻 One of the most important parts of the class was learning about Arrays of Objects using an Employee class example. This helped in understanding: ✔ How objects are created and stored in arrays ✔ The concept of pass-by-reference ✔ How loops can be used to optimize code and avoid repetition when dealing with multiple objects This approach makes programs scalable and efficient, allowing the same logic to work whether we handle 2 objects or thousands of objects. ⚙️ Finally, we explored Command Line Arguments (String[] args), which clarified how Java programs can receive inputs directly from the command line during execution. This concept also introduced how all command-line inputs are treated as Strings, which leads into the next important topic — Java Strings. 📚 Key Takeaways from Today’s Session: • Different ways to create arrays in Java • Arrays with primitive data types and Scanner methods • Handling character input using charAt() • Working with arrays of Strings • Creating and managing arrays of objects • Understanding command line arguments in Java • Writing optimized and scalable code using loops Every session at TAP Academy continues to strengthen my core Java concepts and programming logic, bringing me one step closer to becoming a better developer. 💻✨ #Java #Programming #JavaArrays #LearningJourney #Coding #SoftwareDevelopment #TAPAcademy #JavaDeveloper 🚀
To view or add a comment, sign in
-
-
🚀 Learning Core Java – Understanding Inheritance Today I explored another important pillar of Object-Oriented Programming — Inheritance. Inheritance is the concept where one class acquires the properties (variables) and behaviors (methods) of another class. It is achieved using the extends keyword in Java. This helps in code reusability, reduces duplication, and builds a relationship between classes. ⸻ 🔹 Types of Inheritance in Java Java supports several types of inheritance: ✔ Single Inheritance One class inherits from one parent class. ✔ Multilevel Inheritance A chain of inheritance (Grandparent → Parent → Child). ✔ Hierarchical Inheritance Multiple classes inherit from a single parent class. ✔ Hybrid Inheritance A combination of multiple types. ⸻ 🔎 Important Concept 👉 In Java, every class has a parent class by default, which is the Object class. Even if we don’t explicitly extend any class, Java automatically extends: java.lang.Object This means: • Every class in Java inherits methods like toString(), equals(), hashCode(), etc. • The Object class is the root of the class hierarchy. ⸻ 🚫 Not Supported in Java (via classes) ❌ Multiple Inheritance One class inheriting from multiple parent classes is not supported in Java (to avoid ambiguity). 👉 However, it can be achieved using interfaces. ❌ Cyclic Inheritance A class inheriting from itself (directly or indirectly) is not allowed. ⸻ 💡 Key Insight Inheritance promotes: ✔ Code reuse ✔ Better organization ✔ Logical relationships between classes And remember: 👉 All classes in Java ultimately inherit from the Object class. ⸻ Understanding inheritance is essential for building scalable and maintainable Java applications. Excited to keep strengthening my OOP fundamentals! 🚀 #CoreJava #Inheritance #ObjectOrientedProgramming #JavaDeveloper #ProgrammingFundamentals #LearningJourney #SoftwareEngineering #TechLearning
To view or add a comment, sign in
-
-
🚀 Learning Update: Java Encapsulation, POJO Classes & Real-World Object Handling Today’s live session helped me understand how Encapsulation works practically in Java by building a complete program step-by-step. 🔹 Encapsulation in Java Encapsulation protects data by making variables private and providing controlled access through public methods like getters and setters. 🔹 Building a POJO Class We created an Employee class with: • Private variables (empId, empName, empSalary) • Zero-parameterized constructor • Parameterized constructor • Getter and Setter methods This type of class is called a POJO (Plain Old Java Object) and is widely used in real-world Java applications. 🔹 Understanding the this Keyword The this keyword refers to the currently executing object and helps resolve the shadowing problem when local variables and instance variables have the same name. 🔹 Handling Multiple Objects Efficiently Instead of repeatedly creating objects, we used: ✔ Loops to handle multiple inputs ✔ Arrays of objects to store multiple Employee objects ✔ Scanner input handling to read user input dynamically 🔹 Important Debugging Insight While working with Scanner, I learned about the input buffer problem when mixing nextInt() and nextLine() and how to fix it by flushing the buffer. 🔹 Working with CSV Input & Wrapper Classes We also handled input like: 1,Alex,50000 Using: • split() method to separate values • Integer.parseInt() to convert String to integer • Wrapper classes for type conversion 💡 Key Takeaway Writing programs step-by-step and understanding how objects, constructors, arrays, and input handling work together makes Java concepts much clearer. Excited to keep improving my Core Java and problem-solving skills through continuous practice. #Java #Encapsulation #OOP #POJO #Programming #SoftwareDevelopment #LearningJourney #JavaDeveloper TAP Academy
To view or add a comment, sign in
-
Explore related topics
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