• Post Reply Bookmark Topic Watch Topic
  • New Topic

Throwing and catching exceptions  RSS feed

 
Rj Rajat Jain
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just started learning Java from Heads First Java by O'Reilly, and came across this code -


When I tried to run this, I got an error stating that ScaryException is a "symbol not found". Can anyone help me on this?
Also it'd be great if someone could point me to a reference if the above code is wrong in theoretically.
 
Carey Brown
Bartender
Posts: 2998
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means that the way in which you are running your code does not let the JRE know where to find the ScaryException.class file.

Are you running this from the command line? What did you type?

Are you running this from an IDE?
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Unless you've created a ScracyException.java class somwhere, that code will not execute.  I think this code is not meant to be executed, but is just a way to use exceptions.
 
John Longer
Greenhorn
Posts: 16
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

ScaryException has to be defined by you.


or



You can extend any Exception

In you code you can define 3 types of constructors

Maybe this will help http://stackoverflow.com/questions/1754315/how-to-create-custom-exceptions-in-java
 
Donovan Wilder
Greenhorn
Posts: 1
Chrome Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create the  ScaryException class

ScaryException.java

Make sure the ScaryExceptin.java file is in the same directory as TestExceptions.java file when you compile.


the -cp "." flag tell the compiler and interpreter that the current director is where the class files are located.
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

It is easy enough to create your own Exception classes; I usually create four constructors.
Rj Rajat Jain: Please quote the page number in HFJ, so we can read the original.
 
Rj Rajat Jain
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you everyone for sorting the problem out. I had not declared the Exception class, since it was not taught in the book. I realized we had to declare them when I was doing the exercises related to these.


Campbell Ritchie wrote:Welcome to the Ranch

It is easy enough to create your own Exception classes; I usually create four constructors.
Rj Rajat Jain: Please quote the page number in HFJ, so we can read the original.


Campbell Richie: I am on a kindle, it does not display the page number, corresponding to the original book. Anyways, my problem has been sorted. Thank you guys!!
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rj Rajat Jain wrote:. . . I am on a kindle, it does not display the page number, . . .
Surely it gives chapter and section numbers which allow us to find the page numbers from the table of contents?
 
Rj Rajat Jain
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Rj Rajat Jain wrote:. . . I am on a kindle, it does not display the page number, . . .
Surely it gives chapter and section numbers which allow us to find the page numbers from the table of contents?


Yes it does :-). It was Chapter No. 11, in "Finally: for the things you want no matter what"'s Sharpen your Pencil section.

There is a line that says, assume ScaryException extends Exception, and doesn't put it in the code. I now know, what they meant by that :P
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is on page 328, and as you say, it does tell you to assume there is a suitable Exception class. There is a similar “Code Magnets” exercise on page 349. It includes the Exception class, but it has a different name:-
class MyEx extends Exception { } (sic)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!