Purchase Solution

Java: Throwing Exceptions

Not what you're looking for?

Ask Custom Question

Please help in understanding how Java can someone help answer these two questions regarding about throwing exceptions (see attachment).

Attachments
Purchase this Solution

Solution Summary

The solution answers questions regarding throwing exceptions.

Solution Preview

Question:

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 ...

Purchase this Solution


Free BrainMass Quizzes
Javscript Basics

Quiz on basics of javascript programming language.

Basic Computer Terms

We use many basic terms like bit, pixel in our usual conversations about computers. Are we aware of what these mean? This little quiz is an attempt towards discovering that.

Basic Networking Questions

This quiz consists of some basic networking questions.

Word 2010: Table of Contents

Ever wondered where a Table of Contents in a Word document comes from? Maybe you need a refresher on the topic? This quiz will remind you of the keywords and options used when working with a T.O.C. in Word 2010.

Basic UNIX commands

Use this quiz to check your knowledge of a few common UNIX commands. The quiz covers some of the most essential UNIX commands and their basic usage. If you can pass this quiz then you are clearly on your way to becoming an effective UNIX command line user.