• 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

Diff between throw and throws

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All

What is the diff between throw and throws.

Regards

Nikhil
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You use "throws" to say that a method may throw a certain type of exception.
You use "throw" to actually throw an exception.

http://java.sun.com/docs/books/tutorial/essential/exceptions/throwing.html
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class ExecptionClass{
static void getException() throws NullPointerException{
throw new NullPointerException();
}
public static void main(String... vArgs) throws NullPointerException{
getException();
}
}

throws is generally escalating the exceptions to the invoking method and further even to the JVM.

throw is used to create exceptions at runtime;
eg: throw new ArithmeticException();

The exception object is either catched by the try/catch block if present or thrown by using throws.
 
reply
    Bookmark Topic Watch Topic
  • New Topic