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

  Design an interface interactive game

First, design an interface InteractiveGame that generalizes the playing of any two-person game between a human player and the computer. Design two actual games, Nim and another game of your choice, except TicTacToe, that implement your Interactive..

  Despite public and private health insurance programs

Why is it that despite public and private health insurance programs, some US citizens are without health coverage?

  Java code using jframe

Write a Java code using JFrame. This code needs to manipulate 4 shapes(2 rectangles and 2 squares) in a 400x400 JFrame. This needs to manipulate the shapes in 3 different ways, using 3 different methods. One for translating a shape, one for proportio..

  Feature of object-oriented programming

Use the Internet or Strayer databases to research the advantages, features, and common examples of OOP and EDP. Describe at least one (1) feature of object-oriented programming that Visual Logic lacks

  Display the value of the tostring method of a customer type

display the value of the toString method of a customer type

  Describe the graphical coordinate system in java

Describe the graphical coordinate system in Java. Where is the origin? What units apply to the x,y coordinates? How would you use the Graphics class to draw a line between 2 specific points? Give an example

  Definition of constant initialization arrays

You have to extend the definition of constant initialization arrays to multi-dimensional arrays

  Program that takes the amount a person has spent

Program that takes the amount a person has spent and determines the discount ($15 per $175 spent).

  Using a linked implementation of graph write a method

Write a method that takes two nodes as input and returns true if joining an edge between these two nodes, forms a duplicate path to one of the input nodes within the graph.

  Create an array that will store temperatures

Create an array that will store 7 temperatures. Populate the array with 7 random temperatures from 1 to 100 degrees. (hint use a for loop and a Random number Generator)

  Executing stringed musical instrument class

Create and execute a stringed musical instrument class.

  Writing a program to calculate state income tax

The second programming project involves writing a program to calculate state income tax for a single taxpayer that uses a GUI. The GUI should use the grid layout manager and should looks similar to the following: The salary income, interest income..

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