• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java exception handling

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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" );
}
}
}
 
Ranch Hand
Posts: 81
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is only an example.
 
Wesleigh Pieters
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");
}
}
}
 
Marshal
Posts: 79178
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to you all.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You’re welcome
I hope you found what you needed.
 
Oh the stink of it! Smell my tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic