Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

the keyword throw and throws

yanghui1023yanghui1023 Member Posts: 24
I'm confused about the keywords throw and throws.Is there anybody being able to tell me the usage of throw in the method body and throws int method statement line?
eg:
int devide( ) throws ArithmeticException{

int i=10;
int j=0;
System.out.println("i="+i+"j="+j);
System.out.println(i/j);

}//end of method devide

int devide1(){
try{
throw new ExceptionNew();
//ExceptionNew is an Exception I defined myself.
}//end of try
catch(ExceptionNew e){

``````
}//end of catch

}//end of method devide1

Can anybody tell the difference and usage of them?
By the way what is API short for?









Comments

  • VilanyeVilanye Member Posts: 684
    : I'm confused about the keywords throw and throws.Is there anybody being able to tell me the usage of throw in the method body and throws int method statement line?
    : eg:
    : int devide( ) throws ArithmeticException{
    :
    : int i=10;
    : int j=0;
    : System.out.println("i="+i+"j="+j);
    : System.out.println(i/j);
    :
    : }//end of method devide
    :
    : int devide1(){
    : try{
    : throw new ExceptionNew();
    : //ExceptionNew is an Exception I defined myself.
    : }//end of try
    : catch(ExceptionNew e){
    :
    : ``````
    : }//end of catch
    :
    : }//end of method devide1
    :
    : Can anybody tell the difference and usage of them?
    : By the way what is API short for?
    :
    :
    API == application program interface

    throws goes at the end of the method header. It just tells the compiler and whatever calls it, that a certain exception is thrown. That way the calling method knows that it will either have to catch it or throw it back to whatever method called it. It recursively propagates back through all the method calls, and if it is never caught, it will eventually end up in main and crash the program.

    throw actually throws the exception and it will either have to be caught in that method or propagate back to the calling method via the throws keyword.



    [italic][blue]Just my 2 bits[/blue][italic]

Sign In or Register to comment.