This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes java exception handling Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "java exception handling" Watch "java exception handling" New topic
Author

java exception handling

Nitya Sundar
Greenhorn

Joined: Nov 13, 2012
Posts: 15
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

Joined: Nov 14, 2012
Posts: 10
//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

Joined: Nov 14, 2012
Posts: 10
//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

Joined: Sep 04, 2012
Posts: 81
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

Joined: Nov 14, 2012
Posts: 10
This is only an example.
Wesleigh Pieters
Ranch Hand

Joined: Sep 04, 2012
Posts: 81
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

Joined: Nov 14, 2012
Posts: 10
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

Joined: Oct 13, 2005
Posts: 32694
    
    4
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

Joined: Nov 13, 2012
Posts: 15
Thanks to you all.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32694
    
    4
You’re welcome
I hope you found what you needed.
 
 
subject: java exception handling
 
Similar Threads
Why is My Program Running in parts?
what are the use of APache common lang exception and nested exception over java exception
while loop
Exceptions
Saving JavaMail error to .txt file