Question

Explain when code should throw an exception and when it should catch an exception:

Explain when code should throw an exception and when it should catch an exception:

Homework Answers

Answer #1

Exception is an Runtime error thrown by the system.. So in few scenarios programmer might need to thrown an exception like when user inputs invalid data than in code we use the keyword throw and raise the exception manully

catch: We should catch the exception when ay of the statements inside the try is raising the exceptions or any where in the code we are throwing the exception

Example throw:

throw new NullPointerException();

Example catch:

try{

int a=10/0;

}

catch(ArithmeticException e){}

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
What is exception propagation? Give an example of a class that contains at least two methods,...
What is exception propagation? Give an example of a class that contains at least two methods, in which one method calls another. Ensure that the subordinate method will call a predefined Java method that can throw a checked exception. The subordinate method should not catch the exception. Explain how exception propagation will occur in your example.
What is the output of the following code segment? public class Exception { ... static int...
What is the output of the following code segment? public class Exception { ... static int divider(int x, int y) { try { return x/y; } catch (ArrayIndexOutOfBoundsException a) { System.out.print("A"); return 1; } } static int computer(int x, int y) { try { return divider(x,y); } catch (NullPointerException b) { System.out.print("B"); } return 2; } public static void main(String args[]){ try { int i = computer (100, 0); } catch (ArithmeticException c) { System.out.print("C"); } } } ABC A...
Start with the code below and complete the getInt method. The method should prompt the user...
Start with the code below and complete the getInt method. The method should prompt the user to enter an integer. Scan the input the user types. If the input is not an int, throw an IOException; otherwise, return the int. In the main program, invoke the getInt method, use try-catch block to catch the IOException. import java.util.*; import java.io.*; public class ReadInteger { pubilc static void main() { // your code goes here } public static int getInt() throws IOException...
7.6 LAB: Exception handling to detect input String vs. Integer The given program reads a list...
7.6 LAB: Exception handling to detect input String vs. Integer The given program reads a list of single-word first names and ages (ending with -1), and outputs that list with the age incremented. The program fails and throws an exception if the second input on a line is a String rather than an Integer. At FIXME in the code, add a try/catch statement to catch java.util.InputMismatchException, and output 0 for the age. Ex: If the input is: Lee 18 Lua...
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...
(T or F) The reserved word throw is used to signal that an exception has occurred....
(T or F) The reserved word throw is used to signal that an exception has occurred. (T or F) The correct use of throw can be anywhere in a program; a try / catch structure is used only in class definitions. (T or F) In C++, class is not a reserved word. (T or F) Class attributes have no fixed type; their type can be changed during the program execution. (T or F) A member of a class can be...
What will be the output of the given code? using System; class MyProgram { static void...
What will be the output of the given code? using System; class MyProgram { static void Main(string[] args) { try { int a, b; b = 0; a = 5 / b; Console.WriteLine("No exception will occur."); } catch (ArithmeticException e) { Console.WriteLine("Exception occurs."); } finally { Console.WriteLine("Program is executed."); } Console.ReadLine(); } } A Program is executed. B No exception will occur. C Exception occurs. Program is executed. D No exception will occur. Program is executed.
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
When using Spark SQL to run a query, what happens if you get a parse exception?...
When using Spark SQL to run a query, what happens if you get a parse exception? Explain
When learning how to develop code for methods what skill should i obtain when wanting to...
When learning how to develop code for methods what skill should i obtain when wanting to critically think how to wrtie code based on that method. I understand fundamentals of java coding but i struggle a lot of how to critically think how to write code for methods and projects.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT