Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

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?
0
Eindoofus
Asked:
Eindoofus
  • 3
  • 2
2 Solutions
 
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now