Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (113)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Over the top Error handling?  (Read 325 times)
0 Members and 1 Guest are viewing this topic.
Offline Gibbo3771
« Posted 2014-04-09 13:02:04 »

I am in the process of fool proofing 2 time based classes I have created.

Currently any error made by the user causes a run time exception with a given detail, but how far is too far? Should I be using normal exceptions and letting the user catch the error? The things that will cause the error are common sense, should not really occur.

For example.

A user creates a new clock, they set it as a 12 hour clock. They then try to set the time to 17:00:00 (HH:MM:SS), this throws a run time exception telling them that value is out of scope for a 12 hour clock.

This seems like a valid reason for throwing such an error, but in what cases should I be letting the user catch it?

I was thinking in a situation like so:

User creates a Stopwatch, sets the time to 5 seconds and then tries to reset it without stopping it, this causes weird behaviour (it resets the countdown time, the last update time and the current total time in milliseconds, resetting while running could cause the clock to run forever if the user coded it wrong). Should I let them catch this error and stop it in the catch block?

Or simply do nothing? It is clear they want to reset it but maybe they forgot to reset it.

When should I use each? I understand that a run time exception should be thrown if the program simply can't do without the attempted code execution, and normal exceptions are for non dependent code/user input errors.

What are the exceptions (no pun intended) when deciding which to use?

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline Danny02
« Reply #1 - Posted 2014-04-09 13:33:28 »

evertime someone (a developer) is using your lib in a wrong way it is an programming error and you should throw an unchecked exception which hopefully terminates the app as quick as possible. Checked Exception should only be used for real "exceptions" something which is not a programming error (e.g. "a file doesn't exist").

ps: you should never use exceptions for logic
Offline kalkitus

Senior Newbie


Medals: 1
Exp: 6 years



« Reply #2 - Posted 2014-04-09 14:56:11 »

Sorry for not answering the question...but why wouldn't I be able to set the hour of a 12 hour clock to 17:00:00 and have your program parse it accordingly? That seems like a big restriction for little to no benefit.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Gibbo3771
« Reply #3 - Posted 2014-04-09 15:12:27 »

Sorry for not answering the question...but why wouldn't I be able to set the hour of a 12 hour clock to 17:00:00 and have your program parse it accordingly? That seems like a big restriction for little to no benefit.

Actually I was considering doing this.

Why would you create a clock with the constructor:

1  
2  
3  
public Clock(double timeFactor, ClockFormat format)

Clock clock = new Clock(1, ClockFormat.HOUR_12);


Then decide to change it later? Actually you can change it at any time by simply changing the clock format. Fair enough you never seen this constructor until now but still...

But in what situation would you want to parse one format to another? Serious question because I cant think of one lol

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline kalkitus

Senior Newbie


Medals: 1
Exp: 6 years



« Reply #4 - Posted 2014-04-09 18:58:38 »

Perhaps another person is working on the same code base and prefers 24hr time? Not sure! Just seems like an unnecessary step. The less ways a user can mess it up, the better
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 793
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2014-04-09 20:50:05 »

Each class should support the least amount of functionality possible (don't allow changing the Clock format, unless you see a use case). Any time something fishy is going on (stopping while not running, starting while running, etc, etc) immediately fail with an unchecked exception: never allow your objects to reach an undesired/undefined state (running with a timeFactor <= 0.0 or NaN). Sometimes parameter validation results in code duplication, for example if multiple field values implicitly define a single 'state': in such cases you can move the parameter/field validation into it's own method, allowing you to check whether the entire object is in a valid state.

TL;DR:
 - it's the API's role to be strict
 - it's the callsite's role to be lenient

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (16 views)
2014-09-12 09:08:26

Norakomi (50 views)
2014-09-10 13:57:51

TehJavaDev (58 views)
2014-09-10 06:39:09

Tekkerue (27 views)
2014-09-09 02:24:56

mitcheeb (50 views)
2014-09-08 06:06:29

BurntPizza (34 views)
2014-09-07 01:13:42

Longarmx (20 views)
2014-09-07 01:12:14

Longarmx (23 views)
2014-09-07 01:11:22

Longarmx (22 views)
2014-09-07 01:10:19

mitcheeb (31 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!