Explain the concept of try catch blocks

Assignment Help JAVA Programming
Reference no: EM13941044

You will have noticed in the reading about throwing exceptions, that some exceptions (such as java.io.IOException) must be caught by your code whenever you do something that may throw them, otherwise the Java compiler complains. Any method you write that can throw such an exception must be declared as throws ExceptionName. This is the way it works for most exceptions: you must declare that you can throw them, and you must handle them if they can be thrown.

Question 1:

Why would Java require this? What are some benefits of requiring that exceptions be either explicitly handled or rethrown?

Other exceptions (such as java.lang.ArrayIndexOutOfBoundsException and java.lang.NullPointerException) can be used or ignored freely: any method can throw them anytime, and you don't have to catch them (although you can if you want to).

The difference is that ArrayIndexOutOfBoundsException and NullPointerException are both subclasses of java.lang.RuntimeException. Java makes a special case for any exception that is derived from RuntimeException: it can be thrown at any time, you don't have to declare that you will throw it, and you don't have to catch it if it is thrown.

Question 2:

Why is there this special case for some exceptions? What is different about something like ArrayIndexOutOfBoundsException that makes it beneficial to not have to enclose every statement that could produce it with a try/catch?

Reference no: EM13941044

Questions Cloud

Vest in human resources development : Why organisations should invest in human resources development: Is investing in your own people better than outsourcing".
Calculate the maximum bending stress on a rectangular beam : Calculate the maximum bending stress on a rectangular beam 12 in. x 20 in. that has an applied bending moment of 10,000 lb-in.
Proposal of intended research approach : Please note: You are not required to undertake the project work, merely submit a proposal of intended research approach.
Identifying the style of leadership : Critically evaluate the cultural issues which should be considered when identifying the style of leadership which should be adopted by these new managers.
Explain the concept of try catch blocks : Why would Java require this? What are some benefits of requiring that exceptions be either explicitly handled or rethrown?
Define a sla : 1. Define a SLA 2. How does an SLA add value to you as a facilities manager on site.
Identify two types of connection trap : A publishing company produces scientific books on various subjects. The books are written by authors who specialize in one particular subject. The company employs editors who, not necessarily being specialists in a particular area, each take sole ..
Discuss evolution of urban gang within the united states : Urban gangs have been around for some time. Provide a brief timeline of and discuss the evolution of the urban gang within the United States. Does urban gang membership seem to be growing or declining? Provide evidence.
Customer mind to increase its purchase : Describe a product you like that you believe more people should purchase. As a marketer, how would you reposition the product in the customer's mind to increase its purchase? Outline your strategy.

Reviews

Write a Review

JAVA Programming Questions & Answers

  Java program to compute invoice for client

You need a java program to compute the invoice for client and then print out invoice to the screen. The consulting rate is $200 per hour.

  Write java program to receive input and displayed interest

Write a Java program that receive some input and displayed the interest. Format the number into 2 decimal places. Using loop is an advantage.

  Find the smallest complex number

Given a "Complex" class with all the standard methods for complex numbers, design the static method below to find and return the smallest complex number.

  What must a subclass do to modify a private superclass

When a class extends an abstract class, it must provide definition of all the methods that are declared in the abstract class. Otherwise, it gives a compile time error.

  Create a complete java application that meets these

create a complete java application that meets these requirements1. the program has an opening comments section using

  Projectyou will design and deliver a website foes fictional

projectyou will design and deliver a website foes fictional client. the project is split into two partspart a website

  Ood methodology

Your local police department wants to design new software to keep track of people, property, and criminal activity. List at least three classes you think should be in the design. For each class, identify some data members and methods.

  Java program that reads from standard input

Write a JAVA program that reads from the standard input stream (System.in) the order of cards in a deck and outputs who will win as well as the number of rounds played. It is assumed that there are two players, Player 0 and Player 1.

  Implementing dijkstras shortest-path algorithm

Implementing Dijkstras shortest-path algorithm - To improve efficiency of the program, you should add some caching to it

  Delete a random element from an arraylist

Elements in an ArrayList and picking one at random to remove. Unfortunately, this slow, since deleting a random element from an ArrayList is slow because of all the shifting. For this question, you should modify the poll()method so that it runs in..

  Algorithm analysis with advanced data

algorithm analysis with advanced data structuresalchemy-mines of oreland yield unlimited amounts of silver but no gold.

  Creating a grade book that will use arrays

You will be creating a grade book that will use arrays to store students and their grades. The breakdown of the project is as follows. You must be able to add a student, remove a student, edit a student, remove all students, and exit the program

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd