• Post Reply Bookmark Topic Watch Topic
  • New Topic

A little Exception trouble...  RSS feed

 
Steven YaegerII
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9082
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
Steven YaegerII
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!