Question

In: Computer Science

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.

Please Don't copy from someone else.

Solutions

Expert Solution

"Try Catch block is used to handle any exceptions smoothly. "

I will explain this concept in a simple and understandable way for you.

Let's write a program that divides a number.

SEE THE EXAMPLE:

import java.util.Scanner;

public class ExceptionDemo
{
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);

        // read a number here
        System.out.print("Enter a number: ");

        // Take the number into 'num' variable
        int num=scanner.nextInt();

        int divisionAnswer=10/num;
        System.out.println("The Answer is: "+divisionAnswer);
        System.out.println("Program has completed..!!");
    }
}

If we enter ZERO (0), we will get an exception and the program terminates abnormally.

====================

If we don't use any exception handling techniques, we will get exceptions and errors and our program will terminate abnormally.

====================

USE TRY-CATCH BLOCK for handling such type of exceptions.

import java.util.Scanner;

public class ExceptionDemo
{
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);

        // read a number here
        System.out.print("Enter a number: ");

        // Take the number into 'num' variable
        int num=scanner.nextInt();

        // This Code may raise an error.
        // So, keep it in try block
        try
        {
            // perform a simple division
            // If we divide a number by ZERO, we will get an Exception
            int divisionAnswer=10/num;
            System.out.println("The Answer is: "+divisionAnswer);
        }
        catch(ArithmeticException e)
        {
            // If there is any exception, the control comes here.
            System.out.println("Division By ZERO Exception. Can't divide a number by Zero.");
        }

        System.out.println("Program has completed..!!");
    }
}

====================================

OUTPUT:


Related Solutions

Provide a description of Geico insurance company that you work for. As part of your description...
Provide a description of Geico insurance company that you work for. As part of your description include a discussion of the type of organizational structure.
Raising an Exception In the previous problem, you used a try/except statement to catch an exception....
Raising an Exception In the previous problem, you used a try/except statement to catch an exception. This problem deals with the opposite situation: raising an exception in the first place. One common situation in which you will want to raise an exception is where you need to indicate that some precondition that your code relies upon has not been met. (You may also see the assert statement used for this purpose, but we won’t cover that here.) Write a function...
What is Try and Catch in Java with example?
What is Try and Catch in Java with example?
create your own function that wraps it with try catch. Pass in the string to parse,...
create your own function that wraps it with try catch. Pass in the string to parse, and the name of the field, put some logging in the catch How do I do this? say with this code segment? void readStudents() { //Scanner class object declare Scanner readStudentFile = null; //try block begin try { //open file readStudentFile = new Scanner(new File(".\\src\\test\\student.txt")); //loop until end of file while(readStudentFile.hasNextLine()) { String stu = readStudentFile.nextLine(); String[] eachStu; eachStu = stu.split(" "); students.add(new Student(eachStu[0],...
Write a try/catch/finally clause to catch one or more specific Exception objects in Java.
Write a try/catch/finally clause to catch one or more specific Exception objects in Java.
You must provide a brief description of the obstacle you design, highlighting the physical concepts involved...
You must provide a brief description of the obstacle you design, highlighting the physical concepts involved and explicitly stating the relevant formulas that are at work. If there’s a specific strategy that would be most effectively for completing the obstacle, include a description of it. Using: Rotation Thank you so much.
Java try and catch problem public void method1(){ int[] array = new int[1]; try{ array[2] =...
Java try and catch problem public void method1(){ int[] array = new int[1]; try{ array[2] = 0;} catch(ArithmeticException e){array[2] = 0} // ? finally{ return 0;}} Could you please tell me why the line with the question mark will not report an error?
ldentify the anatomy of the lymph systems as part of your description
ldentify the anatomy of the lymph systems as part of your description
Provide a description of our home galaxy, the Milky Way. Include in your description the location...
Provide a description of our home galaxy, the Milky Way. Include in your description the location and stellar populations (new stars/ old stars) of the central bulge, disk, and halo. Also provide details such as the Hubble classification, disk structure, location of star forming regions, and the best estimate of the number of spiral arms.
A brief explanation is needed for these true/false questions. a) A method that contains a try-catch-finally...
A brief explanation is needed for these true/false questions. a) A method that contains a try-catch-finally structure may also have a “throws” declaration. b) An arithmetic exception such as division by zero can be avoided by careful programming while an I/O exception such as file not found may occur regardless of the precautions taken by the programmer. c) Lets assume that you create an object x of the Object class. You can assign any object (objects of other classes) to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT