• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating custom exceptions  RSS feed

 
Kara Wilimas
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code below is suppose to validate what is in the try/catch statement.
I am having problems in the exceptions. The strings and numbers have different criteria to conform to and I don't know how to write this code so that it checks against the validations and outputs the correct exception statement. I tried if/else the try/catch, to check all validations and and if/else in the else of the first if/else to check the different kinds of validations and to output different exceptions.
Also, really really dumb question. The CDTEST and CDExceptionTEST are suppose to be in the same folder? And I'm not sure what that means.




And this is the Exception.java program

 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You haven't told us what error you are getting so there isn't much we can do to help at the minute.

Having said that, I can see at least one compiler error (well technically several but they are all the same sort of error). In the main method you are referencing member variables of your class (in the first if statement), but this is not allowed because main is a static method. Those member variables only exist inside an instance of the class.
 
Kara Wilimas
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies, I currently get 12 errors.

CDTEST.java:14: error: illegal start of expression
public static void main (final String[ ] arg) throws Exception {
^
CDTEST.java:14: error: illegal start of expression
public static void main (final String[ ] arg) throws Exception {
^
CDTEST.java:14: error: ';' expected
public static void main (final String[ ] arg) throws Exception {
^
CDTEST.java:14: error: illegal start of expression
public static void main (final String[ ] arg) throws Exception {
^
CDTEST.java:14: error: ';' expected
public static void main (final String[ ] arg) throws Exception {
^
CDTEST.java:14: error: illegal start of expression
public static void main (final String[ ] arg) throws Exception {
^
CDTEST.java:14: error: ';' expected
public static void main (final String[ ] arg) throws Exception {
^
CDTEST.java:14: error: not a statement
public static void main (final String[ ] arg) throws Exception {
^
CDTEST.java:14: error: ';' expected
public static void main (final String[ ] arg) throws Exception {
^
CDTEST.java:14: error: not a statement
public static void main (final String[ ] arg) throws Exception {
^
CDTEST.java:14: error: ';' expected
public static void main (final String[ ] arg) throws Exception {
^
CDTEST.java:48: error: reached end of file while parsing
} //program ends
^
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are missing a closing curly brace '}' at the end of your constructor. The main method is technically inside the constructor so the compiler doesn't understand what to do with it. It's not legal do declare a method inside another or inside a constructor.

The big hint in those error messages that there is a missing brace comes from the last error message, where it says it reached the end of file while parsing. This means the compiler was still trying to match up the curly braces when it ran out of lines in the file.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kara Wilimas wrote:Apologies, I currently get 12 errors.
CDTEST.java:14: error: illegal start of expression
public static void main (final String[ ] arg) throws Exception {
^
...
CDTEST.java:48: error: reached end of file while parsing
} //program ends
^

And that's because the method before your main() doesn't have a closing brace.

The clue is that last error: Whenever you get a "reached end of file" message, it usually indicates that your braces don't match up.

Winston

[Edit] Damn! Too slow.
 
Kara Wilimas
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
< The CDTEST and CDExceptionTEST are suppose to be in the same folder? And I'm not sure what that means. >
23 errors now. Most of them will be solved if I figure that ^^ out?
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly don't you understand about it? It means that your two .java files need to be in the same folder when you compile them.

That isn't a general requirement of Java, its common to put .java files in different directories. But the directory structure has to match the package structure that the classes have been declared to belong to using the package statement. Have your learned about packages yet?

In your case there don't appear to be any package statements in the code you posted (it would be the first line of the class). That means that both classes are in the default, unnamed package.

Technically there are ways to make it compile if the classes are in different directories, but for the sake of simplicity it is easier for the minute to put them in the same directory.
 
Kara Wilimas
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. Errors now:
error: non-static variable artistname cannot be referenced from a static context
error: cannot find symbol
MyException stocknegative = new MyException ();
method getMessage in class Throwable cannot be applied to given types;
albumblank.getMessage("Album name contains no characters");
error: cannot find symbol
if(albumname.length > 0){


 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you get that sort of error message you should move out of a static context as soon as possible. Look at Winston's FAQ about the main method. Create an object and then move everything into that object. That should sort out that error.

As for the error about illegal strt of type: you have already been told that is usually caused by too few }s. Search my posts for “write backwards” and you will find my suggestions to prevent unmatched {}.
 
Kara Wilimas
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, thank you. Another error question...
How can I fix this type of error?
CDExceptionTEST.java:9: error: cannot find symbol
launcher.run(arg);
^
symbol: method run(String[])
location: variable launcher of type CDExceptionTEST

I am getting multiple errors like this.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kara Wilimas wrote:
CDExceptionTEST.java:9: error: cannot find symbol
launcher.run(arg);
^
symbol: method run(String[])
location: variable launcher of type CDExceptionTEST

That means that the compiler can't find that method to call on the launcher variable. Since launcher appears to be a CDExceptionTEST, it means that class doesn't have a run() method that takes a String array as an argument. Or, if it does, it isn't visible from where it is being called.

If you're getting lots of errors, that sounds like you're trying to write a lot of code and then compile it. You should be compiling often, that way you catch this sort of error and fix them as you go along.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!