• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

throws exception

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

There are times when it makes sense for a program to deliberately throw an exception. This is the case when the program discovers some sort of exceptional or error condition, but there is no reasonable way to handle the error at the point where the problem is discovered. The program can throw an exception in the hope that some other part of the program will catch and handle the exception.



http://www.faqs.org/docs/javap/c9/s3.html

May someone give me simple example of such case?
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just about anything. A pre-condition not being met, wrapping an underlying exception into something more application specific, validation issues, ...
 
Sheriff
Posts: 22656
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Marshal
Posts: 75879
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote: . . . will throw NullPointerException if name == null . . .

It's usually a good idea to throw a NullPointerException for null inputs.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And it will? (Unless your point was to make it explicit.)
 
Rob Spoor
Sheriff
Posts: 22656
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Campbell means this: if an empty name is allowed but a null name isn't then manually throwing an NPE is a good practice:
 
Campbell Ritchie
Marshal
Posts: 75879
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:I think Campbell means this: . . .

Yes, he does
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All right,

From what I understand, We use try/catch while we deal with a code that may throw exception

But We use throw when we want the code throws exception, right now!


and there is a catch clause for this exception that throws by "throw keyword" explicitly .

I saw these codes in wikipedia:




As you, the throw keyword is used now in catch clause.
How the exception throws in this caught?

When is it necessary to use throw in a catch clause?



This is what I understand:

You have a code that might throw exception, You put this code in try clause, Then exception caught by catch clause.

But there are sometimes that you need throw exception explicitly,then you use throw key word, and there is a catch clause that waaits to process this exception.Then, the throw key word must not be in a catch, It must be out of catch.

Please correct me If I'm wrong

 
Campbell Ritchie
Marshal
Posts: 75879
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Be careful to distinguish the keywords throw and throws. They are used in different contexts, however; the compiler will notice the difference.

It is usually a bad idea to throw and catch an exception in the same place. So you have a method which might suffer something going wrong, and it will include something likeIn order that any methods using that method can handle that Exception, you mark the method withNow any method using it (if it is a checked Exception) must use try-catch, or another throws clause.

By the way: it is usually a bad idea to log and throw the same Exception.
Unchecked Exceptions are better handled with @throws tags in their documentation comments than with the throws keyword.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In this code, You didn't use any catch, Does JVM catch this exception?

Could you put a catch in your code to show how catch this exception by catch clause?
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JVM catches any exception that isn't caught by your own code. Yes, you can catch a NullPointerException.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




java.lang.ArithmeticException: / by zero
at exceptionthrows.ExceptionDemo.divideByZeroAndPrint(ExceptionDemo.java:22)
at exceptionthrows.Main.main(Main.java:19)
java.lang.ArithmeticException: / by zero
BUILD SUCCESSFUL (total time: 0 seconds)





It must print : "Integer division by 0"

But it does not!Why?

 
Campbell Ritchie
Marshal
Posts: 75879
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code is incorrectly indented, still.

java ExceptionDemo
Exception in thread "main" java.lang.ArithmeticException: Integer division by 0
at ExceptionDemo.divideByZeroAndPrint(ExceptionDemo.java:12)
at ExceptionDemo.main(ExceptionDemo.java:21)

I can't see what is wrong with it. Have you saved and recompiled your Java code before execution?
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This code must print: Integer division by 0

But it does not, Why?

It is said the statement you use in throw keyword will be printed
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If something catches and logs the stack trace, it will, and Campbell provided output that shows this happening.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by " logs the stack trace"

Again, I don't understand why this code does not print what is wrote in throw clause?
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It does.
 
First, you drop a couch from the plane, THEN you surf it. Here, take this tiny ad with you:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic