Question

In: Computer Science

What is Try and Catch in Java with example?

What is Try and Catch in Java with example?

Solutions

Expert Solution

A TRY block contains the program statement in which an exception can occur. Also a TRY block is always followed by CATCH block which is used to handle the exceptions that occur in TRY block. A TRY block is always followed by CATCH block.

A CATCH block is associated with a TRY block and the corresponding CATCH block is executed if an exception occurs in the try block.

Below is the JAVA Program to Explain the Try Catch Code

class TryCatch {
   public static void main(String args[]) {
     int number1, number2,number3;
     try {
        // Try block to handle code that may cause exception
        number1 = 0;                   //We Assign Number 1 with 0        
        number2 = 65;                  //We Assign Number 2 with 65
        number3 = number2 / number1;   //Now we try to divide 65 by 0 which should through an exception
                                       //and will be caught in catch block
        System.out.println("Try block message");
     } catch (ArithmeticException e) {
            // This block is to catch exception for divide-by-zero error
            System.out.println("Error: Don't divide a number by zero");
       }
     System.out.println("I'm out of try-catch block in Java.");
   }
}

When Exception Caught


Related Solutions

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.
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?
*IN JAVA* EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2. Define...
*IN JAVA* EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2. Define an exception class and call it from the driver program. For this lab you will complete two parts. Part (a) of the lab implements try… catch and uses an existing exception in C# or in Java. Write a program that implements an ArrayIndexOutOfBounds error. Part (b) writes a program that converts a time from 24-hour notation to 12-hour notation. Assume the user will enter...
Modify the following program. Add using Exception handing and polymorphism. try-catch block, Finally block in java...
Modify the following program. Add using Exception handing and polymorphism. try-catch block, Finally block in java * description of class Driver here. *these is the main class where it acts like parent class. Compiler will execute firstly static method. import java.util.Scanner; public class Driver {     public static void main (String[] args){         Scanner stdIn = new Scanner(System.in);         String user;         String computer;         char a = 'y';         do {             System.out.println("What kind of Computer would you like?");...
How does try/catch(exception) processing help resolve run time exceptions? What is a custom exception? What are...
How does try/catch(exception) processing help resolve run time exceptions? What is a custom exception? What are it's benefits? What does it mean when we raise a custom exception?
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],...
OBJECT-ORIENTED JAVA The catch-or-declare requirement states that you have two choices: You can either catch the...
OBJECT-ORIENTED JAVA The catch-or-declare requirement states that you have two choices: You can either catch the exception or you can provide a throws clause. To which exceptions does the catch-or-declare requirement apply? Errors All Exceptions except RuntimeExceptions RuntimeExceptions All Throwable except Errors Flag this Question Question 21 pts Assume you have an array called numbers. It's elements are of type int and it was declared like this: int[] numbers = { 3, 6, 9, 12 }; Assume that you also...
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...
(in C# please.) EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2....
(in C# please.) EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2. Define an exception class and call it from the driver program. For this lab you will complete two parts. Part (a) of the lab implements try… catch and uses an existing exception in C# or in Java. Write a program that implements an ArrayIndexOutOfBounds error. Part (b) writes a program that converts a time from 24-hour notation to 12-hour notation. Assume the user will...
Basic JAVA Question When we try to run this program, a problem occurs. What is this...
Basic JAVA Question When we try to run this program, a problem occurs. What is this problem called? And why does this problem occur here? public class Main {    public static void main(String[] args) {    PhoneNumber pn = new PhoneNumber();        System.out.println(pn.isTollFree());    } } class PhoneNumber { String number;    public boolean isTollFree() { return number.charAt(1) == '8'; } }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT