Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Customer exception

  1. #1
    Junior Member
    Join Date
    Apr 2024
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Customer exception

    public class MainClassCutsom {

    public static void main(String[] args) throws CustomExceptionClass{

    Scanner in = new Scanner(System.in);
    System.out.println("Enter the player name");
    String playerName = in.nextLine();
    System.out.println("Enter the player age");
    Integer age = Integer.parseInt(in.nextLine());
    CustomExceptionClass c = new CustomExceptionClass(playerName, age);

    try {
    if(c.getAge() >= 19) {
    c.displayDetails();
    } else {
    throw new CustomExceptionClass("InvalidAgeRangeException");
    }
    } catch(Exception e) {
    System.out.println("CustomException:"+e.getMessage ());
    }
    in.close();
    }
    }

    I just want to know that the way i constructed this code is a good practice or not. Becaus, when i used only throws and throw keyword the output which i got in the console is in red lines and track race, which i don't want that way to see my output. For that I've used this catch block to print the right message which i got like this below.

    Output:
    ---------
    Enter the player name
    kumar
    Enter the player age
    12
    CustomException:InvalidAgeRangeException

  2. #2
    Member
    Join Date
    Jan 2024
    Posts
    63
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Post Re: Customer exception

    Using try-catch blocks to handle exceptions is a common practice, especially when you want to provide more specific error messages or handle exceptions gracefully without showing stack traces directly to the user. However, it's essential to handle exceptions appropriately based on the context and requirements of your application.

    In your case, you're catching `Exception` which is a general catch-all for any type of exception. While this ensures that your program won't crash unexpectedly, it's generally recommended to catch more specific exceptions whenever possible. This allows you to handle different types of exceptions differently if needed.

    For instance, in your code, you're throwing a `CustomExceptionClass` for invalid age range. It would be better to catch only `CustomExceptionClass` rather than catching all exceptions. This ensures that you're only handling exceptions that you anticipate and explicitly handle.

    Here's a modified version of your code with a more specific catch block:

    ```java
    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Enter the player name");
    String playerName = in.nextLine();
    System.out.println("Enter the player age");
    Integer age = Integer.parseInt(in.nextLine());
    CustomExceptionClass c = new CustomExceptionClass(playerName, age);

    try {
    if (c.getAge() >= 19) {
    c.displayDetails();
    } else {
    throw new CustomExceptionClass("InvalidAgeRangeException");
    }
    } catch (CustomExceptionClass e) {
    System.out.println("CustomException: " + e.getMessage());
    } finally {
    in.close();
    }
    }
    ```

    In this version, we catch only CustomExceptionClass, ensuring that we handle only the specific exception we expect to encounter. Additionally, I've moved the in.close() call to the finally block to ensure that the Scanner is always closed, even if an exception occurs. This is good practice to avoid resource leaks. If you find yourself needing further help with Java assignment, there are plenty of online resources available like ProgrammingHomeworkHelp.com assignment help platform, where you can seek guidance and support.

  3. #3
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: Customer exception

    sounds like an answer from ChatGpt
    Whatever you are, be a good one

Similar Threads

  1. Assignment - Question attached. not sure how to add customer class
    By Mattz0r in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 24th, 2014, 05:19 PM
  2. Customer java program( Whats wrong with my code?)
    By Blazta in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 29th, 2013, 03:37 AM
  3. Customer counting program - while loop
    By Shenaniganizer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 11th, 2012, 02:56 PM
  4. LOOping for my Customer ....
    By LogicBrix in forum Java Theory & Questions
    Replies: 3
    Last Post: October 11th, 2011, 06:44 PM
  5. Best way to automatically login user from a customer's web site.
    By ess_stegra in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: February 2nd, 2010, 12:16 PM