jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Qn on Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Qn on Exception" Watch "Qn on Exception" New topic
Author

Qn on Exception

xiao sean
Ranch Hand

Joined: Aug 20, 2007
Posts: 44
hi,

i try debugging this but to no avil.
can anyone advice on the bugs?

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Well, any bugs would be trivial compared to the almost unimaginable horror of throwing up a JOptionPane in the constructor of an exception. Where on earth did you get that idea?

I see some missing parentheses on one of the "throw new" lines. What are the bugs you're talking about?


[Jess in Action][AskingGoodQuestions]
xiao sean
Ranch Hand

Joined: Aug 20, 2007
Posts: 44



hi, thanks for the reply. the error happen inside here. it stated ( or [ is require. however, i checked a few time that the brackets are close.
[ October 16, 2007: Message edited by: xiao sean ]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

1) You're missing a } in the constructor.
2) The UnAcceptableNumberException takes only one integer, not two.
3) The AbsoluteNumberException throwing misses the () and the integer argument.
4) The class is actually called AbsoluteException
5) Where does newBig come from in getBig?
6) setBig misses the newBig parameter
7) toString() misses the return statement: return s;
8) You mistyped both JOptionPane and showMessageDialog in the exception constructors. Actually, you mistyped them EVERYWHERE.
9) The import statement has to go at the top of the file. Only the package statement, comments, whitespace and other import statements can come before import statements.
10) You haven't declared bigNo and smallNo.
11) nt is declared inside the try block, and is not available outside of it.


I haven't even looked at style or anything, this is just syntax. Seriously, please try compiling code before you post it. An IDE such as Eclipse would have shown you all these errors and suggestions on how to fix them.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
xiao sean
Ranch Hand

Joined: Aug 20, 2007
Posts: 44
thanks for the help. because my school only allows us to use blueJ. so i need to compile and debug one by one. that why i struck at the 1st bug before i'm able to carry on..
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Try write code one bug at a time -- i.e., write the smallest possible program that will compile; fix any compiler errors; test it; and only then should you add more. That way you'll know that any error has to do with the last one or two lines that you typed.

Now, even if you get this to compile, it is still an abomination before man and God. Take that JOptionPane stuff OUT OF THE EXCEPTION CONSTRUCTORS, and move it into those empty "catch" blocks! Exceptions are for reporting errors in code; catch blocks are for handling them (and one part of "handling" is "reporting to the user.") Never, never print anything or especially put up a dialog box inside the constructor of an exception class, and never, never write an empty catch block!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Originally posted by Ernest Friedman-Hill:
Never, never print anything or especially put up a dialog box inside the constructor of an exception class, and never, never write an empty catch block!

Agreed. An exception constructor should basically call one of the super constructors as much as possible, and set any local variables. As for the empty catch block, if you don't want to do anything meaningful, at least log the exception, e.g. by calling e.printStackTrace(). That way, you can at least open the application in a console and see what exceptions are being thrown.
xiao sean
Ranch Hand

Joined: Aug 20, 2007
Posts: 44
hi ,everyone i did alot of amendment and changes to the program.

the program seem working fine without test class. however, i' m confuse whether what i have code is right? it's seem wrong to me, however there is a right ouput.
after comparing the values..

my question is actually am i require to output the values in the BloodPressure class or just the exception class will do?

but i'm struck on how do i test whether my program works?

appriecate if anyone able to advice me on that. kind of confuse by my own codes.

p/s: the vaules enter will be assume either low or high.

these are the codes.

Exception class

test class


[ October 17, 2007: Message edited by: xiao sean ]
[ October 17, 2007: Message edited by: xiao sean ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

You're still not using exceptions correctly. Call "checker" from the BloodPressure constructor. Take those print statements out of the Exception constructors and put them into the PressureException catch block. The idea is that code throws an exception to indicate that something is wrong, and some other code catches that exception and does something -- fix the problem, report it to the user, etc.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Qn on Exception
 
Similar Threads
Exception in thread "main" java.lang.NullPointerException
A Special TextEdit control for Date
Validation Problem
Run Time Error
candidates