• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Throwing and catching exceptions

 
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.
 
Bartender
Posts: 6140
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java 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?
 
Sheriff
Posts: 6190
164
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • 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.
 
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
 
Greenhorn
Posts: 1
Eclipse IDE Chrome 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.
 
Marshal
Posts: 65457
248
  • 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: 65457
248
  • 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: 65457
248
  • 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)
 
Without deviation from the norm, progress is not possible - Zappa. Tiny ad:
create, convert, edit or print DOC and DOCX in Java
https://products.aspose.com/words/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!