IntelliJ Java IDE
The moose likes Java in General and the fly likes A little Exception trouble... Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "A little Exception trouble..." Watch "A little Exception trouble..." New topic
Author

A little Exception trouble...

Steven YaegerII
Ranch Hand

Joined: May 31, 2000
Posts: 182
The code below gives 1 error: Exception Problem must be caught or it must be declared in the throws clause of this method. At Line14: method( digit, amount );
Well that's all fine & dandy, I know how to catch it, but I'm just trying to create my own checked exception with class Problem and have it thrown at compile time, by calling static void method( int num, int var ). So, my question is, "What changes do I need to make below so that when I compile it, the Problem exception is thrown and its message is displayed on screen?" Also, is the code structure on the right track or should I move something around (like putting the method( int num, int var ) in class Problem)? Everything I read about exceptions seems to confuse things twice as much as it clears things up.



Thanks, Steve
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9001
You need to have your method "throws Exception", not the code inside the method. I also wonder why you have Roundup extending Exception. Is Roundup going to be an exception that some other class will throw?



JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Steven YaegerII
Ranch Hand

Joined: May 31, 2000
Posts: 182
I started coding again & going through mock exams and coding examples for all the questions that I get wrong. Then I came to the rules roundup question #36 "When you override a method, the overriding method in the subclass must not declare ANY new exceptions not declared in the overriden method?" (t/f).
So I read up a little bit and was going to code a subclass method overriding a base class method that declares a checked exception. Three variations of this would be: Subclass declares the same exception that the base class does, Sub declares no exceptions, and Sub declares a new checked exception. At first I was going to just override one of Java's core methods that throw a checked exception but since I got stuck so bad, I decided to first make my own exception class (Problem), then make a method that throws it (static void method(int, int) in class Roundup), and now that I've made it this far, I'll override static void method(int, int) from a subclass of Roundup to apply the three variations listed above.
So, the code is pretty trivial, its only purpose is so I can understand the principles to the questions that I get wrong. Just focusing on the Exception code and not the semantics of the other stuff, does it seem like it is set up how it should be?? I've always put off learning about exceptions, so I don't really know, but it felt kind've redundant while I was coding the exception stuff. (I guess I can remove the extends keyword from the class Roundup declaration.)
Thanks Marilyn
 
 
subject: A little Exception trouble...
 
Threads others viewed
sereral doubs... pls clear them all
Exception
RR#99
Exception's Question
IOException
MyEclipse, The Clear Choice

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture