finally Clause Assignment Help

Assignment Help: >> Exception Types - finally Clause

finally Clause:

Finally, for finally.  Assume there are a few actions which you absolutely must do, no matter what happens. Commonly, this is to free a few external resources after obtaining it, to close a file after opening it, or something same. To be sure that "no matter what" involves exceptions as well; you use a clause of the try statement designed for accurately this sort of thing, finally:

SomeFileClass f = new SomeFileClass();

if (f.open("/a/file/name/path")) {

try {

someReallyExceptionalMethod();

} finally {

f.close();

}

}

This use of finally behaves very much such as the following

SomeFileClass f = new SomeFileClass();

if (f.open("/a/file/name/path")) {

try {

someReallyExceptionalMethod();

} catch (Throwable t) {

f.close();

throw t;

}

}

except that finally could also be used to clean up not only after exceptions but after return, break, and continue statements as well. The complex demonstration:

public class MyFinalExceptional extends Context {

public static void main(String args[ ]) {

int mysteriousState = getContext();

while (true) {

System.out.print("Who ");

try {

System.out.print("is ");

if (mysteriousState == 1)

return; System.out.print("that ");

if (mysteriousState == 2)

break; System.out.print("strange ");

 if (mysteriousState == 3)

continue;

System.out.print("but kindly ");

if (mysteriousState == 4)

throw new UncaughtException();

System.out.print("not at all ");

} finally {

System.out.print("amusing man?\n");

}

System.out.print("I'd like to meet the man ");

}

System.out.print("tell me.\n");

}

}

Here is the output generates that is depending on the value of mysteriousState:

1. Who is amusing man?

2. Who is that amusing man?

Please tell me

3. Who is that strange amusing man?

...

4.  Who is that strange but kindly amusing man?

5.  Who is that strange but kindly not at all amusing man?

I'd like to meet the man Who is that strange...?

...

The exception handling mechanism in Java permits your methods to report errors in a manner which cannot be ignored. Each exception which is thrown must be caught or the application terminates. An Exception is actually class objects derived from the Throwable class. Thus, exceptions merge data and methods; an exception object generally holding a string explaining what the error is.

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