What does the following "Exception" code do? And how can I make it more useful?

public class InvalidDataException extends Exception {

    public InvalidDataException(String msg) {
        super(msg);
    }
}

Open in new window


Here is another chunk of Exception code that I don't understand the purpose of:

public class CannotFitException extends Exception {

    public CannotFitException(String msg) {
        super(msg);
    }
}

Open in new window


I received the above code from my instructor as part of a sample project and I was just wondering what it does. I have no idea what it's doing other than printing the message for an exception, but doesn't the compiler already do that on it's own anyway?

Is there anyway to rewrite this to make it more useful?
EindoofusAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
They're just custom Excpetion classes that would be relevant in a certain programming context
0
EindoofusAuthor Commented:
Do they do anything that the compiler doesn't already do? Is there anyway to redesign them so that I get more feedback when a method throws that type of exception?
0
for_yanCommented:
They allow you to write your own exception message and
Take action specific to your particular situation.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

for_yanCommented:
Java provides ready exceptions which reflect very general situations - like illegal format of the nu$ber - in your own class extending exception you can provide the user with infromation specific to thje flow of your operatio - like the "first operand is in wrong format"
0
CEHJCommented:
Here's an example of one of them in action. The input must only be digits. If you give it the wrong input you get something like


InvalidDataException: Input must be only digits
        at Test.parse(Test.java:21)
        at Test.main(Test.java:7)

import java.util.*;
import java.io.*;

public class Test {
    public static void main(String[] args) {
	try {
	boolean isValid = Test.parse(args[0]);
	}
	catch(InvalidDataException e) {
	    e.printStackTrace();	
	}
    }

    public static boolean parse(String input) throws InvalidDataException {
	boolean result = false;
	try {
	    Integer.parseInt(input);
	    result = true;
	}
	catch(NumberFormatException e) {
	    throw new InvalidDataException("Input must be only digits");
	}
	return result;
    }
}

Open in new window

0
for_yanCommented:
You can of course erite error mesage to the user, but exceptions allow you to throw them - to interrupt flow of the method - you can easily transfr them to the calling methiod to report them to the user in right pace - so they have some conveniences which wrror message does not give you
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.