Handling exceptions Assignment Help

Assignment Help: >> Declaring and handling exceptions >> Handling exceptions

Handling exceptions:

We now know how to indicate that a method throws one or more exceptions. To deal with an exception that has been thrown, we use a try-catch statement. The simplest case is when only one type of exception is expected. For example, if checkFormat is invoked by another method, loadFiles, in the same class:

public void loadFiles ()
{
...
try
{
if (checkFormat("File1.dat"))
{
// code to process file data
}
}
catch (EOFException eofEx)
{
// code to handle this exception
// eofEx refers to EOFException object
}
}

 

The try-catch statement is made up of two parts - the try block and the catch clause. The try block contains the code that may throw an exception. If the exception occurs, the code within the catch clause will be executed; the catch clause handles the exception.

If more than one type of exception is possible, we can add additional catch clauses, like this:

try
{
if (checkFormat("File1.dat"))
{
// code to process file data
}
}
catch (EOFException eofEx)
{
// code to handle end of file exception
// eofEx refers to an EOFException object
}
catch (MalformedURLException urlEx)
{
// code to handle bad URL exception
// urlEx refers to a MalformedURLException object
}
...

The code in the try block is executed; if an exception is thrown the try block is exited and the appropriate exception handler code, if any, is executed. The exception handling code that is executed depends on which exception has been thrown.

The system checks each catch clause in turn for one that matches the type of the exception object. If the exception was an object described by the class EOFException, then the code immediately following:

catch (EOFException eofEx)
{

is executed, and so on. A more subtle point is that this handler will be selected if the exception object is either of type EOFException or of any subclass of EOFException.

When this handler code is executed, the variable within the catch clause will contain a reference to the exception object that has been thrown. For example, in the handler for EOFException the variable eofEx will contain a reference to the exception object.

If the code throws an exception that is not handled in any try-catch statement within the method, then the exception will be passed to the method that invoked this method. Any exception not handled in a particular method will be passed up a chain of invoking methods to a higher level, until an appropriate exception handler is found. This is known as propagating the exception, as shown in Figure. If no handler exists at any level for this exception, then the program terminates with an error message.

453_handling exception.png

Recall that if the code within a method may throw a checked exception then you must do one of two things:

  • c advertise in the method header that an exception may be thrown, and pass it on to the next level to deal with;
  • c catch the exception within the method.

The Java compiler enforces these rules. If the first option is adopted you are assuming that the exception will be propagated to some other method, which will have the same options - to catch and handle the exception or to pass it on. A given method may have both a throws declaration in the header and a try-catch statement within the method - normally these would be for different types of exception. 

Java Assignment Help - Java Homework Help

Struggling with java programming language? Are you not finding solution for your Handling exceptions homework and assignments? Live Handling exceptions experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Handling exceptions homework help, java assignment help and Handling exceptions projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.

Why Expertsmind for assignment help

  1. Higher degree holder and experienced experts network
  2. Punctuality and responsibility of work
  3. Quality solution with 100% plagiarism free answers
  4. Time on Delivery
  5. Privacy of information and details
  6. Excellence in solving java programming language queries in excels and word format.
  7. Best tutoring assistance 24x7 hours

 

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