OCP - 2

Functional Programming, Dates, Strings, Localization, Exceptions and Assertions

Functional Programming, Dates, Strings, Localization, Exceptions and Assertions


Set of flashcards Details

Flashcards 115
Language English
Category Computer Science
Level Other
Created / Updated 22.02.2019 / 08.01.2020
Weblink
https://card2brain.ch/box/20190222_ocp_2
Embed
<iframe src="https://card2brain.ch/box/20190222_ocp_2/embed" width="780" height="150" scrolling="no" frameborder="0"></iframe>

When is a NumberformatException thrown?

By the program when an ettempt is made to convert a string to a numeric type, but the string doent have an appropriate format.

When is a ArithmeticException thrown?

Thrown by the JVM when dividing by zero

What kind of Exceptions from the JDK are typically checked exceptions?

IO, Parsing and SQL-Exceptions

When is a java.text.ParseException thrown?

Used when converting a String to a number

When is an java.io.IOExcepiton thrown?

Used when Dealing wiht IO and NIO.2 issues. IOException is the parent class and there are a number of subclasses.

When is a java.lang.ArrayStoreException thrown?

Used when trying to store the wrong data type in an array

When is a java.util.MissingResourceException thrown?

Used when trying to access a key or resource bundle thet does not exist.

Which of the following exceptions are checked?

Is a try statement required to have either or both of the catch and finally clauses?

Yes, with one exception: Try-with-resources does not require either

Can you declare more than one finally block?

No

What rules apply to the catch clauses?

  • It is ilegal to declare a subclass exception in a chatch block that is lower down in the list than a superclass
  • you cannot catch a checked exception type that cannot potentially be thrown by the try clause

Which of these are valid multi-catch declarations?

Is this code valid?

try{
 // do sth.
}catch(RuntimeException e){
  e = new RuntimeException()
}

Yes, re-assigning catched exceptions is a legacy "feature".

Is this code valid?

try{
 // do sth.
}catch(IOException | RuntimeException e){
  e = new RuntimeException()
}

No, in multi-catch statements, re-assigning catched exception is not allowed!

How can you declare multiple resources in a try-with-ressource statement?

Separate them with a semicolon

In which order are the resources in a try-with-resources block closed?

In the reverse order in which they were declared

What happens if an exception is thrown in a try-with-resource statmement?

It depends on how the try block endend:

  • Try-block ended normally: The exception from the close method is thrown and must either be handeled in a catch block or by the caller (checked exceptions must be declared, of course)
  • Try-block ended with an exception: The exception from the close method is added as a suppressed exception to the one from the try-block.

The implicit finally block fomr a try-with-resources statement runs ___ any programmer-coded ones

The implicit finally block fomr a try-with-resources statement runs before any programmer-coded ones

Is this code valid?

try(Scanner s = new Scanner(System.in)) {
  s.nextLine();
}catch(Exception e){
  s.nextInt();
}finally {
  s.nextInt();
}

 

No, the catch and finally block do not compile because s went out of scope right after the try block

What's the difference between Closeable and AutoCloseable?

AutoCloseable is used in the try-with-resources and is newer and more generic than the Closeable Interface. Closeable declared an IOException whereas AutoCloseable declares an Exception

When no exception is declared on the close method of a specific implementation on AutoClosable, do you have to handle Exception when using the try-with-resources statement?

No, you only have to handle the specified checked exception of the concrete AutoCloseable implementation

What is the idea of suppressed exceptions?

When multiple exceptions occur, eg. in a try block and also when attempting to close a resource, the first exception is assumed th primary one and all other exceptions are attached as suppressed exceptions to the primary one

Is the exception from the finally block handled as a suppressed exception?

try(JammedTurkeyCage t = new JammedTurkeyCage()){
  throw new IllegalStateException("turkey ran off");
} finally {
  throw new RuntimeException("haven't found it");
}

no, Suppressed exceptions apply only to exceptions thrown in the try clause.

The RuntimeException  from the finally block effecively swallows the IllegalStateException, which means that it is lost

What does this code do?
public void rerhrowing() throws SQLException, DateTimeParseException {
  try {
    parseData();
  } catch (Exception e) {
    System.err.println(e);
    throw e;
  }
}

It is a short for the following code to prevent duplicates. Java can infer the specific Exception from the method signature

public void rerhrowing() throws SQLException, DateTimeParseException {
  try {
    parseData();
  } catch (SQLException | DateTimeParseException e) {
    System.err.println(e);
    throw e;
  }
}

 

What are assertions for?

They can verify that sth. is always true. They are intended for development and NOT for production!

What must be considered when using assertions?

  • Because they only run optional, assertions should not have sideeffects!
  • Asserts are disabled by default and must explicitly be enabled

What happens when an assertion fails?

A java.lang.AssertionError is thrown. Since programs aren't supposed to catch an Error, this means that assertion failures are fatal and end the program.

How can you enable/disable assertions?

  • Enable using the JVM -enableassertions (-ea) flag
  • Disable using the JVM -disableassertions (-da) flag

What happens here?

java -ea:com.example.demos... my.programs.Main

Enable assertions in any class in the specified package or subpackage.

Should asserts be used to check for valid arguments passed into a method?

NO, use if-statements and IllegalArgumentExceptions instead!

The variable in a multi-catch expression is ___.

The variable in a multi-catch expression is effectively final

Can you use Closeable in a try-with-resources statement?

YES

How can you specify a message for an assert statement?

Using a colon after the boolean expression, eg:

assert false : "This always goes wrong"

Is IllegalFormatException a checked exception?

No

What happens when calling Optional.of(null)

A NullpointerException is Thrown