• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to throw Exceptions

 
Ranch Hand
Posts: 176
Mac Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Hello everyone,

I have a project to do and my professor wants us to create our own methods that can throw an exception. Not having any idea how to do this I check Java's official site:
http://java.sun.com/docs/books/tutorial/essential/exceptions/throwing.html
I looked at the first example on that page and this something similar to parseExpression(String) without putting throws ParseException. But the complier gave me an error "unreported Exception".
I reread the criteria that we were given and realized that our professor gave us the method signature for parseExpression(String) and that it included "throws ParseException" at the end. So I added that as you can see in the code and it got rid of the syntax errors.

However when I call the method without putting it inside a try{} it compiles are runs, which it isn't meant to, the compiler should normally tell me "expection must be caught or throw". And thus this method doesn't throw a ParseException when there is an error in String expr's format.

Help please I've only got until the end of this week to complete this.

Thanks in advance.
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It should give an error unless you are again using throws in the method where you are calling parseExpression method.
FYI, All exceptions that extends RuntimeException doesn't require any try/catch block.
 
Olivier Legat
Ranch Hand
Posts: 176
Mac Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No it doesn't give an error, that's the problem. Here is the class I used to test:


The first one gives me -2.0 as in should. And the second one gives me -2.5, when it should be giving me an error.
 
Olivier Legat
Ranch Hand
Posts: 176
Mac Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I found my error, it was a problem with my testing code. In the second test I wrote simpleParseExpression instead of parseExpression.

All is good Thanks for your help Sunil
 
Can't .... do .... plaid .... So I did this tiny ad instead:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic