Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

exeption handling

 
Hasitha Randika
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what's the output of following code, will it give a compilation error,why?


public void divide(int a, int b) {

try {

int c = a / b;

} catch (Exception e) {

System.out.print("Exception ");

} finally {

System.out.println("Finally");

}
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi hassel

What i can see from the code is

the variables a and b are method variables
can say local so they dont get any default value
and
if we try to use

method variables before
initialisation ie before assigning them any value
we get a compiler error

The above code will not compile


I hope this helps!!!
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by dhwani mathur:
...the variables a and b are method variables
can say local so they dont get any default value...

The variables a and b are local to the method, but they are parameters. They need to be passed values in order for the method to be called.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Hassel:
what's the output of following code, will it give a compilation error,why? ...

This will not compile because it's only a method definition. If it were inside a class, then it would compile (assuming the missing brace were added).

To get output, you will need a main method that creates an instance of the class and calls this method. The output will depend on what int values you pass.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic