learningbydeveloping.com

 

Learning by developing

...because practice teaches you

PDF Version: scjp T-5

When overriding methods, the new methods can throw more specific exceptions than the ones they override, but never broather. It may, even not throw anything(maybe it try-catches the exceptions from the overriden method).

Try-catch-catch-finally.: It can have as many catches as is needed.

Finally method: this will be executed always, even if an exception is thrown. The only exception is if System.exit() is executed. Useful for doing one action always(E.g.: saving some data). Note: If in the question asks for output with finally and says that there might be some code non-specified, think about that code having “System.exit()”.

Exception notes: If a method throws an exception and another method calls it. This method needs either to declare that it throws that exception or try-catch the exception.If it is propagated until main, it will throw a runtime exception and show the stacktrace. It is also needed, if you are EXPLICITLY throwing the exception(throw new Exception()). You can add, after the word “throws” multiple exceptions sepparated by commas:

public void method() throws myException1, myException2, aserejeException3{}

Two types of exceptions: JVN thrown and programmatic thrown. The main exceptions are:

JVM Exceptions

Programmatic Exceptions

NullPointerException

IllegalARgumentException

StackOverflowError

IllegalStateException

ArrayIndexOutOfBoundsException

NumberFormatException

ClassCastException

AssertionError

ExceptionInitializerERror

StackOVerflowError

NoClassDefFoundError

 

The main runtime and checked exceptions are:

Runtime Exceptions

Checked Exceptions

ArrayIndexOutOfBoundsException

ClassNotFoundException

ClassCastException

URISyntaxException

IllegalArgumentException

IOException

IllegalStateException

FileNotFoundException

NumberFormatException

Non-reachable code would not compile. So, for instance, non reachable code after throwing(by code) one exception, wouldn’t compile.

Assertions: assert(a>b):”blah blah”;If the expressions evaluates to FALSE, then an AssertionExceptionwill be thrown and it would carry a message with “blah blah”.

Note: throw new Exception() is correct.

Note: is correct to say: assert(a>b); without anything else

It is also correct to declare it somehow like:

assert(a>b):methodX();

But methodX(); MUST return a value(even if it returns “new Object()”

Asertions must be enabled with java -ea: Iclase(enable assertions)

They can be disabled as well: java -da :Miclase (disable assertion)

Or enable for one and disable for anotherone: java -ea -da:EnabledClass DisabledClass

Note: It is appropiate to use assertion in private methods but not in public ones and, definitively, assertions should not betry-catched in order to change the program state. Assertions should not check public methods arguments(e.g.: mains args).

Important: Main method may throw an exception, but the excetion MUST be imported(E.g.: throw IOException, should import java.io.*;).

Exceptions and Errors:

Exception is NOT a superclass of Error(they are two different classes which inherit from Throwable).

An infinite loop causes StackOverflowError (not exception), so if you want to catch it, you should catch an error, else it would throw it and, probably, terminate the program.

Most important exceptions:

NumberFormatException extends IllegalArgumentException

Some of the most important exceptions are:

ArrayIndexOutOfBounds

ClassCastException

IllegalStateException

NullPointerException

NumberFormatException

AssertionError

ExceptionInitializerError

StackOverflowError

NoClassDefFoundError

Please, check the definition of these exceptions in the java API.

 
**********************************************

The books I have used are these ones, which I strongly recommend. You can support this blog buying them through this links:

 

and

 

For OCJP version 7, I would recommend:

 

 

Categories: Java

Leave a Reply