Question

Provide a description of the try .. catch statement. As part of your description you must...

Provide a description of the try .. catch statement. As part of your description you must describe how this statement can be used to handle errors and exceptions and include an example of try ... catch implemented to handle an error or exception within a Java program.

Homework Answers

Answer #1

//EXAMPLE PROGRAM

// Exception Handling

// Simple try-catch block

class Demo

{

public static void main(String args[])

{

System.out.println("\n\t *** Program Start ***");

try

{

int a = Integer.parseInt(args[0]);

int b = Integer.parseInt(args[1]);

int c=a/b;

System.out.println("\n\t Division = "+ c);

}

catch(Exception e)

{

System.out.println("\n\t *** Exception caught ***");

System.out.println(e);

}

System.out.println("\n\t *** Program End ***");

}

}



OUTPUT :

[root@localhost ADON]# java Demo 8 4

*** Program Start ***

Division = 2

*** Program End ***
[root@localhost ADON]# java Demo 8 0

*** Program Start ***

*** Exception caught ***
java.lang.ArithmeticException: / by zero

*** Program End ***
[root@localhost ADON]# java Demo

*** Program Start ***

*** Exception caught ***
java.lang.ArrayIndexOutOfBoundsException: 0

*** Program End ***
[root@localhost ADON]#











Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In JAVA For practice using exceptions, this exercise will use a try/catch block to validate integer...
In JAVA For practice using exceptions, this exercise will use a try/catch block to validate integer input from a user. Write a brief program to test the user input. Use a try/catch block, request user entry of an integer, use Scanner to read the input, throw an InputMismatchException if the input is not valid, and display "This is an invalid entry, please enter an integer" when an exception is thrown. InputMismatchException is one of the existing Java exceptions thrown by...
You are asked to implement a C++ class to model a sorted array of unsigned integers....
You are asked to implement a C++ class to model a sorted array of unsigned integers. The class is to be used in an embedded application that cannot assume the presence of the STL. The array has to be dynamically allocated in such a way that allows programmers using it to specify the required size. Your class should should: (1) provide the appropriate constructors and destructor; (2) provide methods for updating, and showing numbers in/to the array (e.g., to be...
For a home repair job you must turn the handle of a screwdriver 24 times. Part...
For a home repair job you must turn the handle of a screwdriver 24 times. Part A If you apply an average force of 14 N tangentially to the 3.0-cm-diameter handle, how much work have you done? Express your answer to two significant figures and include appropriate units. Part B If you complete the job in 22 seconds, what was your average power output? Express your answer to two significant figures and include appropriate units.
Write a program in Java that: 1. will prompt user with a menu that contains options...
Write a program in Java that: 1. will prompt user with a menu that contains options to: a. Add a To-Do Item to a todo list. A To-Do Item contains: i. An arbitrary reference number (4; 44,004; etc.) ii. A text description of the item ("Pick up dry cleaning", etc.) iii. A priority level (1, 2, 3, 4, or 5) b. View an item in the list (based on its reference number) c. Delete an item from the list (based...
Assume you are the administrator of the healthcare facility and must provide a Power Point presentation...
Assume you are the administrator of the healthcare facility and must provide a Power Point presentation on your facility's sustainability program to community leaders, including local leaders from the manufacturing plant, that have been violating emission standards and negatively impacting air quality. Also assume that these leaders are supporters of your healthcare facility and thus provide sizeable donations. How do you handle the content of your power point presentation? Typed please
In this paper you will focus on the following: 1. Provide a description of the company...
In this paper you will focus on the following: 1. Provide a description of the company at which you work(ed). 2. Describe an “agency problem” (not just any problem) within the firm and discuss what you think is causing the problem and how the problem might be better controlled. 3. Describe the job dimensions of the firm and discuss whether or not you believe the current design is appropriate for the firm. Discuss any suggestions you might have for improving...
For part 1, you will NOT be writing anything in Python. You will be describing how...
For part 1, you will NOT be writing anything in Python. You will be describing how the program will work using words and illustrations if it helps. Using a dictionary, make a text encrypter/decrypter. In decrypt mode, if the user inputs “Y r u l8?”, the program should output “Why are you late?”, for example. In encrypt mode, if the user inputs “See you later”, it should output “c u l8r”. Obviously you can’t handle every possible phrase, but add...
1. Given the following multi-way if statement, provide a switch statement, using proper java syntax, that...
1. Given the following multi-way if statement, provide a switch statement, using proper java syntax, that will provide the same function. Char grade; String tstmsg; if (grade == ‘A’) {   tstmsg = “Excellent”; } else if (grade == ‘B’) {   tstmsg = “Good”; } else if (grade == ‘C’) {   tstmsg = “OK”; } else {   tstmsg = “Study More”; } 2.Write the following for statement as a while statement. for (k = 0; k < 3; k++) {   System.out.println...
*OBJECT ORIENTED PROGRAMMING* * JAVA PROGRAMMING* Create a program that simulates a race between several vehicles....
*OBJECT ORIENTED PROGRAMMING* * JAVA PROGRAMMING* Create a program that simulates a race between several vehicles. Details don't matter must have the following: Design and implement an inheritance hierarchy that includes Vehicle as an abstract superclass and several subclasses. Include a document containing a UML diagram describing your inheritance hierarchy Include at least one interface that contains at least one method that implementing classes must implement. Include functionality to write the results of the race to a file; this will...
Practice using the technique of Elicit/Provide/Elicit (Explore/Offer/Explore) in conversation. Select the best example of EPE you...
Practice using the technique of Elicit/Provide/Elicit (Explore/Offer/Explore) in conversation. Select the best example of EPE you used. Please give a brief description (2 pts) of what you and your conversation partner said for each part of this technique. Describe how it helped (or did not) your interaction (2 pts), and any insights as to how you could improve using this technique in the future (1 pt).