Question

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.*;

public class sentence {
    public static void main() throws IOException {
        InputStreamReader br = new InputStreamReader(System.in);
        BufferedReader in = new BufferedReader(br);
        System.out.println("Enter a sentence.");
        String a = in.readLine();
        String ar[] = new String[10000];
        int k = 0;
        try {
            if (a.length() == 0) {
                throw new NullError();
            }
            if ('a' < a.charAt(0) && a.charAt(0) < 'z') {
                throw new CaseError();
            }
            if (a.charAt(a.length() - 1) != '.') {
                throw new FullStopError();
            }
            int countuppercase = 0;
            String s1 = "";
            for (int i = 0; i < a.length(); i++) {
                char c = a.charAt(i);
                if (1 < countuppercase) {
                    throw new CaseError();
                }
                if (c != ' ' && c != '.') {
                    s1 = s1 + c;
                } else {
                    ar[k] = s1;
                    k++;
                    s1 = "";
                }
            }
        } catch (NullError e) {
            System.out.println("You can't enter an empty string.");
        } catch (CaseError e) {
            System.out
                    .println("You have used the upper case and lower case properly.");
        } catch (FullStopError e) {
            System.out.println("You have forgot to use the full stop.");
        }
        for (int i = 0; i < k - 1; i++) {
            String tmp = "";
            for (int j = i; j < k - 1; j++) {
                if (ar[j].length() > ar[j + 1].length()) {
                    tmp = ar[j];
                    ar[j] = ar[j + 1];
                    ar[j + 1] = tmp;
                }
            }
        }
        for (int i = 0; i < k; i++) {
            System.out.print(ar[i]);
            if (i < (k - 1)) {
                System.out.print(" ");
            } else
                System.out.print(".");
        }
    }
}

class NullError extends Exception// If someone asks me what is name or purpose
                                    // of defining this class what should I
                                    // tell?
{
    public NullError() {
    }
}

class CaseError extends Exception {
    public CaseError() {
    }
}

class FullStopError extends Exception {
    public FullStopError() {
    }
}

For example if I write this code.

What is the name of a class which extends the Exception class is it called an abstract class and what is its use?

This code arranges the words in sentence in increasing order of the number of letters, I implemented some custom exceptions which will be forcefully thrown, such as if there is no full stop. To throw a new exception we need to define a separate class with the same name. I don't understand what is the name of this class is it an abstract class and whats is purpose since I am only defining an empty constructor for the class.

Was it helpful?

Solution

No, this is not called abstract class.

Class that extends Exception can be called user defined checked exception. You can find more information on this topic at http://docs.oracle.com/javase/tutorial/essential/exceptions/creating.html

Just for you to know, abstract class is something completely different and unrelated to exceptions. More information on that: http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html

Hope that helps! :)

OTHER TIPS

What you have written is a custom exception that is a user defined exception. These are exception which pertain to your business logic. These exception classes can be thrown from your business logic. These are useful when there are public classes which have methods which throw these exceptions. The other classes which use these public methods have the choice to either catch the exception and perform operation according to the exception like logging it or you can throw the same exception again.

This way using user defined exception helps the developer of the modular class to not bother about how to handle the exceptions. The handling of the exception will lie with class calling the method which has throws in its signature.

You can not see the importance of user defined exceptions because you just have one class and you are throwing and even catching the exception.

To create a custom/self exception:

public class customException extends Exception { 
  String message = "Your Custom Exception"
 public customException() {
    super(message);
}

}

its a normal class which extends the parent class ( Exception ). To forcefully throw and catch your custom exception:

try
{
   throw new customException();
}     

catch (customException e) 
{
   System.out.println(e);
}

Your code will then print your exception.

Abstract class: No its not an abstract class. This doc defines abstract class : http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top