Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java exception handling

 
Nitya Sundar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please any body help me out?what is the role of compiler and JVM when an exception arises in a java program?
 
uzair ahmed
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//may be this document could help you
-->classAdd without Exception
{ {
public static void main( String[] arguments)
{
intnumber1;
intnumber2;
intsum;
number1 = Integer.parseInt(arguments[0]);
number2 = Integer.parseInt(arguments[1]);
sum = number1 + number2;
System.out.printf( "Sum is %d\n", sum );
}
}

-->classAdd with Exception

public class Add
{ {
public static void main( String[] arguments)
{
intnumber1;
intnumber2;
intsum;
try {
number1 = Integer.parseInt(arguments[0]);
number2= IntegerparseInt(arguments[1]); number2 = Integer.parseInt(arguments[1]);
sum = number1 + number2;
System.out.printf( "Sum is %d\n", sum );
} catch (Exception e) {
System.out.printf( "You did something terrible!\n\n" );
}
}
}
 
uzair ahmed
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//May be this documant could help
public class Add
{ {
public static void main( String[] arguments)
{
intnumber1;
intnumber2;
intsum;
try {
number1 = Integer.parseInt(arguments[0]);
number2= IntegerparseInt(arguments[1]);
sum = number1 + number2;
System.out.printf( "Sum is %d\n", sum );
} catch (Exception e) {
System.out.printf( "You did something terrible!\n\n" );
}
}
}
 
Wesleigh Pieters
Ranch Hand
Posts: 81
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
uzair what are you trying to say?

the compiler wont compile source code if there is a checked exception that is not handled or declared. while you can, the compiler does not require you to handle runtime exceptions.
 
uzair ahmed
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is only an example.
 
Wesleigh Pieters
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
uzair ahmed wrote:This is only an example.


you need to fix the example as that will never compile and will be very confusing to them.
 
uzair ahmed
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.util.Scanner;
public class Square
{
public static void main( String[] args)
{
try
{
Scanner input = new Scanner( System.in);
int number;
int square;
System.out.print("EnteranInteger:");
number = input.nextInt(); //check for integer and then for character
square = number * number;
System.out.printf( "Square of %d is %d\n", number, square );
}
catch (Exception e)
{
System.out.printf( "You did something terrible!\n\n" );
} finally
{
System.out.printf( "Do Something regardless of exception!\n");
}
}
}
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
uzair ahmed wrote://may be this document could help you . . .
I don’t think it does help at all, I am afraid.

Apart from your posting unformatted, uncompilable code (read this), your exception handling is very poor. Using Exception, when you can predict what sort of Exceptions you might suffer, and that non‑specific error message, and not good style at all.

Nitya Sundar: welcome to the Ranch and sorry you have been given such poor advice. For exceptions, start reading here in the Java Tutorials. That should answer your question; if it doesn’t be sure to ask again
 
Nitya Sundar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to you all.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
I hope you found what you needed.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic