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
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...
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.
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.
When the physician documents suspected or possible avian influenza, a code from category J09 should be...
When the physician documents suspected or possible avian influenza, a code from category J09 should be reported. True or False?
What is the minimum modification needed to make the code that follows compile? public void readData()...
What is the minimum modification needed to make the code that follows compile? public void readData() {     boolean dataNotFound = true;     if (dataNotFound) {         throw new IOException();     }     catch(IOException e) {         System.out.println("data file can't be found");     } } a. Place the if statement within a try block. b. Add a throws clause to the method declaration. c. Set dataNotFound to false. d. No modification is needed.
When you throw a ball horizontally, the vertical component of the ball’s speed ... A. continuously...
When you throw a ball horizontally, the vertical component of the ball’s speed ... A. continuously increases. B. remains zero. C. continuously decreases. D. first decreases and then increases. E. remains constant When you throw a ball horizontally, the horizontal component of the ball’s speed ... A. remains constant. B. remains zero. C. continuously increases. D. continuously decreases. E. first decreases and then increases. I know the answers are both A, but can someone explain why??
Describe cost-benefits analysis and explain when is should be used and when it should be avoided.
Describe cost-benefits analysis and explain when is should be used and when it should be avoided.
Economic Growth Model Why does the economic growth model predict that poor countries should catch up...
Economic Growth Model Why does the economic growth model predict that poor countries should catch up to rich countries in income per capita ? Have poorer countries been catching up ( converging) with richer countries ? Explain.
Does the “coin throw” problem follow a binomial distribution? Explain why.
Does the “coin throw” problem follow a binomial distribution? Explain why.