Java Custom Exception Handling with User Defined RadiusException

🔹 Java Concept – User Defined (Custom) Exception Today I practiced creating my own Custom Exception in Java instead of using only built-in exceptions. In real applications, sometimes Java’s predefined exceptions are not enough. So we can create our own exception class based on our business rule. 🔹 What I implemented I created a class: RadiusException extends Exception This exception is thrown whenever a negative radius is given to a Circle object. 🔹 Program Logic • Created a Circle class with a radius variable • If radius is positive → calculate Area & Perimeter • If radius is negative → throw RadiusException So the program checks: 👉 A circle cannot have negative radius 🔹 Methods I tested printArea() → calculates area printPerimeter() → calculates perimeter If radius < 0: Program throws and catches my custom exception and prints a proper message instead of crashing. 🔹 What I learned • How to create a User Defined Exception • class MyException extends Exception • Using throw keyword to raise exception • Using try-catch to handle it • Validating data using programming rules This made me understand that exceptions are not only for system errors… We can also use them to enforce real-world constraints inside programs ✔ Special thanks to my mentors for guidance Saketh Kallepu Anand Kumar Buddarapu Uppugundla Sairam @Codegnan #Java #CustomException #ExceptionHandling #OOP #JavaProgramming #CodingPractice #LearningJourney

  • text

To view or add a comment, sign in

Explore content categories