Java Exercise 5 :Write a constructor for this class that takes a String argument and stores it. Write a method that prints out the stored String. An "Input is not a String" exception should be thrown if the input is not a string.


public class StringException extends Exception{
private String str;
public StringException(String str){
public String toString(){
return "Enter your name correctly";
// exception message here you can type whatever you want

// Next Class

import java.util.*;
public class UserDefinedException{
public static void main(String[] arg) throws Exception{
String str = getstring();
        Boolean flag= false;
for(int i=0; i<str.length();i++){
if (((str.charAt(i) < 91) && (str.charAt(i) > 64))||(str.charAt(i) < 123) && (str.charAt(i) > 96))
           throw new StringException(str);
            if(flag == true)
            System.out.println("Your name is " + "  "+ str);

static String getstring(){
Scanner input=new Scanner(;
System.out.println("enter your name");
String m;
return m;


// Output

enter your name
Your name is   Faizal


enter your name
Exception in thread "main" Enter your name correctly
at UserDefinedException.main(


  1. This blog awesome and i learn a lot about programming from here.The best thing about this blog is that you doing from beginning to experts level.

    Love from

  2. I have read your blog its very attractive and impressive. I like it your blog.

    Java Online Training Java EE Online Training Java EE Online Training Java 8 online training Core Java 8 online training

    Java Online Training from India Java Online Training from India Core Java Training Online Core Java Training Online Java Training InstitutesJava Training Institutes


Copyright © Rough Record. Designed by OddThemes