• Post Reply Bookmark Topic Watch Topic
  • New Topic

School project help  RSS feed

 
Chris McBurn
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


TEST PROGRAM



Why will by throw exception not work? when I try this code in a test program I get the error:

unreported exception Exception; Must be caught or declared to be thrown
 
Campbell Ritchie
Marshal
Posts: 55760
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by going through the Java™ Tutorials about exceptions, particular where it says catch or notify. If you throw a plain simple Exception, that is checked, and the compiler will force you to handle it somehow. Otherwise the program will not compile.
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not only follow the sage advice already given, but what IDE do you use? It seems to me that my NetBeans IDE tells me what exception needs to be caught while I am coding.
 
Chris McBurn
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I left out the throw exception out and just used a if-else println error... I think it's in the requirements of my assignment to use a throw exception but I don't know how to get it to work.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exceptions must be caught or declared. To catch them, use try/catch. To declare them, put a throws clause in the method declaration.
 
Chris McBurn
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a throws clause at the top of the method?
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True, but exceptions "bubble up" so you need catch or declare the exception in MovieAccountTest.
 
Campbell Ritchie
Marshal
Posts: 55760
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never use == true or == false which are not only poor style but also error‑prone. If you write = by mistake you can get two logic errors for the price of one.
Not
if (b == true) ...
but
if (b) ...
Not
if (b == false) ...
but
if (!b) ...
You have much repeated code in that snippet; lines 6-19 are almost identical to lines 23-37. Don't use decimal format. That was superseded by Formatter over eleven years ago. Don't use \n unless somebody tells you they need an LF character. Use printf and the %n tag. Don't use += for Strings. That has poor performance, though you won't notice until you use it many times. Use + instead.
 
Liutauras Vilda
Marshal
Posts: 4650
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And probably you want to simplify your method "streamMovie" which does all sort of things which are not related with the method name.

And since it is a school assignment, you suppose to successfully loose some marks of not using right methods for the right tasks (one task per one method). Actually you have been told that by Campbell R., who said you're repeating yourself by writing duplicated code - it is actually the same thing just in different words, which requires the same approach in order to fix it.
So, if you see you write duplicated code - make it separate method and reuse. If you see you write code which is not streaming movie - move that code away and make a method, then use it.

Package name according to the Java convention suppose to start be in lower cases (you have differently at the moment).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!