Skip to main content

Posts

Showing posts with the label Exception Handling

Write a program to accept name and age of a person from the command prompt(passed as arguments when you execute the class) and ensure that the age entered is >=18 and < 60. Display proper error messages. The program must exit gracefully after displaying the error message in case the arguments passed are not proper. (Hint : Create a user defined exception class for handling errors.)

Write a program to accept name and age of a person from the command prompt(passed as arguments when you execute the class) and ensure that the age entered is >=18 and < 60.  Display proper error messages.   The program must exit gracefully after displaying the error message in case the arguments passed are not proper.  (Hint : Create a user defined exception class for handling errors.) //If you have any alternate method then comment below. public class ExceptionHandling5 { public static void main(String[] args) { String name = args[0]; int age = Integer.parseInt(args[1]); try { if(age < 18 || age >= 60) throw new InvalidAgeException("Invalid Age"); System.out.println("Name: "+name+" Age: "+age); } catch (InvalidAgeException e) { System.out.println(e); } } } class InvalidAgeException extends Exception { public InvalidAgeException(String age) { super(age); } } Write a Program to tak...

A student portal provides user to register their profile. During registration the system needs to validate the user should be located in India. If not the system should throw an exception. Step 1: Create a user defined exception class named “InvalidCountryException”. Step 2: Overload the respective constructors. Step 3: Create a main class “UserRegistration”, add the following method, void registerUser(String username,String userCountry) with the below implementation • if userCountry is not equal to “India” throw a InvalidCountryException with the message “User Outside India cannot be registered” • if userCountry is equal to “India”, print the message “User registration done successfully” Invoke the method registerUser from the main method with the data specified and see how the program behaves. Example1) i/p:Mickey,US o/p:InvalidCountryException should be thrown. The message should be “User Outside India cannot be registered” Example2) i/p:Mini,India o/p:User registration done successfully

A student portal provides user to register their profile. During registration the system needs to validate the user should be located in India. If not the system should throw an exception.  Step 1: Create a user defined exception class named “InvalidCountryException”. Step 2: Overload the respective constructors. Step 3: Create a main class “UserRegistration”, add the following method, void registerUser(String username,String userCountry) with the below implementation • if userCountry is not equal to  “India” throw a InvalidCountryException with the message “User Outside India  cannot be registered” • if userCountry is equal to  “India”,  print the message “User registration done successfully”  Invoke the method registerUser from the main method with the data specified and see how the program behaves. Example1) i/p:Mickey,US  o/p:InvalidCountryException should be thrown. The message should be “User Outside India  cannot be registered”  Exampl...

Write a Program to take care of Number Format Exception if user enters values other than integer for calculating average marks of 2 students. The name of the students and marks in 3 subjects are taken from the user while executing the program. In the same Program write your own Exception classes to take care of Negative values and values out of range (i.e. other than in the range of 0-100)

Write a Program to take care of Number Format Exception if user enters values other than integer for calculating average marks of 2 students. The name of the students and marks in 3 subjects are taken from the user while executing the program. In the same Program write your own Exception classes to take care of Negative values and values out of range (i.e. other than in the range of 0-100) //If you have any alternate method then comment below. import java.util.Scanner; public class ExceptionHandling3 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); for (int i=0; i<2; i++) { String StudentName = ""; int sub1 = 0; int sub2 = 0; int sub3 = 0; try { StudentName = sc.nextLine(); if (sc.hasNextInt()) sub1 = sc.nextInt(); else throw new NumberFormatException(); if (sc.hasNextInt()) sub2 = sc.nextInt(); else throw new NumberFormatException(); i...