Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8071655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:45:27+00:00 2026-06-05T13:45:27+00:00

I did a custom made exception but I am not sure if it is

  • 0

I did a custom made exception but I am not sure if it is correct and also I don’t know why is a case of an exception the program is not printing the message for the exception but only the name of the class for the custom exception, and also how can I make it in a case of an exception to terminate the program because in the moment the program only informs about the exceptions and go on. The program is compiling and running on eclipse. Cheers guys. Here is the code.

import java.util.Scanner;
public class Stud
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter age of the student");
        int age = input.nextInt();
        try
        {
                if(age < 7 || age > 18)
                {   
                    throw new InvalidStudentException("Age can't be less than 7 and more than 18");
                }
        }
        catch(InvalidStudentException e)
        {
                System.err.println(e) ;
        }

        System.out.println("Enter the gender of the student");
        String gender = input.next();
        try
        {
            if((!gender.equals("male")) || (!gender.equals("female")))
            {
                throw new InvalidStudentException("Gender can be only male or female");
            }
        }catch(InvalidStudentException e)
        {
            System.err.println(e) ;
        }

        System.out.println("Enter the name of the student");
        String name = input.next();
        try
        {


            if(name.length() < 10 || name.length() > 50)
            {
                throw new InvalidStudentException("The length of the name must be greater than 10 and less than 50 characters");
            }
        }catch(InvalidStudentException e)
        {
            System.err.println(e) ;
        }

        System.out.println("Enter the class number for the student");
        int classNumber = input.nextInt();

        try
        {
            if(classNumber < 1 || classNumber > 11)
            {
                throw new InvalidStudentException("Class number can be only between 1 and 11");
            }
        }catch(InvalidStudentException e)
        {
            System.err.println(e) ;
        }
    }
}

/** the class with the custom made exception
 */
public class InvalidStudentException extends Exception
{
    String msg;
    int num;

     public InvalidStudentException() 
     {
         super();
     }

     public InvalidStudentException(String msg) 
     {
         super();
         this.msg = msg;
     }
     public InvalidStudentException(int num) 
     {
         super();
         this.num = num;
     }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T13:45:28+00:00Added an answer on June 5, 2026 at 1:45 pm

    You are catching the exception so the program will move on in exceptional cases too. You could rethrow using throw it from catch block or use System.exit()

    To print the details of exception

    use

    e.printStackTrace()
    

    instead of System.out.println(e);


    Also See

    • Catching and Handling Exceptions
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I moved some hardcoded settings to a custom Section (so I did not use
I already did my studies, I know how to make a custom formatter, or
I have made a custom UISwitch (from this post) . But the problem is,
I have made a custom trackbar control, mostly as an exercise. I know i
I know, there are already a lot of similar questions here but I did
I need a custom layer (extended by new variable and a method) and did
I wanted to make a basic custom keypad for the iPhone , I did
Did you ever have the following situation: you need to store information, but a
Did about 30 minutes worth of searching, found lots of relevant info, but none
Did I not get enough sleep or what? This following code var frame=document.getElementById(viewer); frame.width=100;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.