This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Comprehensive Example for Exeption handling  RSS feed

 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


------------------
 
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please find my explanation on a similar topic here
Thanks,
Sandeep
 
Thomas Markl
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!