Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Comprehensive Example for Exeption handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Comprehensive Example for Exeption handling" Watch "Comprehensive Example for Exeption handling" New topic
Author

Comprehensive Example for Exeption handling

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
Hello Javaranchers,
I have constructed a good example of ways to handle an
Exeption.
I appreciate your comments and further explanations.
Here is the code:
================================================================
public class Q13a {
static int call(int x) //throws Exception {
{
try
{
System.out.println(x---x/0);
return x--;
}
catch(Exception e)
{
return x;
}
}
public static void main(String[] args) //throws Exception
{
System.out.println(" value = "+ call(5));
}
}

My Question regards the �throws Exeption� statement.
- If I comment it out in line2, then the Exeption is handled in the try-catch-clause. The result of this code is �value = 4� as the ArithmeticExeption (Division by Zero) is caught by the catch-clause and instead returning the Exeption to the caller the return value is returned to the caller (see www.javaranch.com/ubb/Forum23/HTML/011100.html). If the Exeption is handeled in try-catch of Q13a then I don�t need to pass on the Exeption from Q13a (�throws Exeption� Statement in line 2). And as no exeption is passed on to MAIN the MAIN method doesn�t need to handle it either by passing it on to JVM (throws exeption in MAIN method not neccessary) or to handle it by a try-block in MAIN.
==============================================================================
// Exeption in Q13b is neither handeled in method call nor handeled
// in MAIN method by a try-catch-clause. Instead it is passed on
// from call to MAIN (throws Exception in line2) and from MAIn to
// JVM by throws Exeption in MAIN-clause.
public class Q13b {
static int call(int x) throws Exception
{
// try
// {
System.out.println(x---x/0);
return x--;
// }
// catch(Exception e)
// {
// return x;
// }
}
public static void main(String[] args) throws Exception
{
System.out.println(" value = "+ call(5));
}
}
Result: JVM compiles but when you run program the exeption is printed: �Exeption in thread MAIN.java.lang.Arithmetic Exeption Division by zero�
// Exeption in Q13c is not handeled in method call by a try-catch-
// clause. Instead it is passed on to MAIN by "throws Exception"-
// statement.
// In MAIN the method isn't passed on (to JVM) by another method
// but handeled with try-catch statement.
// The exeption is caught and message "Error in MAIN" is
// printed. Then MAIN goes ahead and prints "Program is
// continued". This means when the superior programm
// (here MAIN) calls a method (here method "call") which
// is disrupted by an Exeption and the exeption is caught
// by the superior programm the superior programm can
// go ahead with its work.
// In this case the next MAIN statement
// System.out.println("Program is continued after catch-clause");
// is executed and MAIN is continued.
public class Q13c {
static int call(int x) throws Exception
{
// try
// {
System.out.println(x---x/0);
return x--;
// }
// catch(Exception e)
// {
// return x;
// }
}
public static void main(String[] args) throws Exception
{
try {
System.out.println(" value = "+ call(5));
}
catch (Exception e)
{
System.out.println("Error in MAIN");
}
System.out.println("Program is continued after catch-clause");
}
}
Result:
Error in MAIN
Program is continued after catch-clause


------------------
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Please find my explanation on a similar topic here
Thanks,
Sandeep


<b>Sandeep</b> <br /> <br /><b>Sun Certified Programmer for Java 2 Platform</b><br /> <br /><b>Oracle Certified Solution Developer - JDeveloper</b><br /><b>-- Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java </b><br /><b>-- Object-Oriented Analysis and Design with UML</b><br /> <br /><b>Oracle Certified Enterprise Developer - Oracle Internet Platform</b><br /><b>-- Enterprise Connectivity with J2EE </b><br /><b>-- Enterprise Development on the Oracle Internet Platform </b>
Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
I forgot one case:
// Exception is cauhgt by method "call" in catch-clause.
// But it is transformed in another exception and then
// passed on by "throws exception" to superior method (MAIN).

public class Q13d {
static int call(int x) throws Exception
{
try
{
System.out.println(x---x/0);
return x--;
}
catch(Exception e)
{
throw new Exception ("This is my Arithmethic exception");
// Catches exception System.out.println and processes it in catch-clause which the throws new exception.
}
}
public static void main(String[] args) throws Exception
{
try {
System.out.println(" value = "+ call(5));
}
catch (Exception e)
{
System.out.println("Caught Exception: " + e.getMessage());
// or System.out.println("Caught Exception: " + e);
}
System.out.println("Program is continued after catch-clause");
}
}
Result:
Caught Exception: This is my Arthimtic Exception
Program is continued after catch-clause.
 
 
subject: Comprehensive Example for Exeption handling
 
Similar Threads
exception handling
Exception
Exception
exception trouble
exception doubt