Jump to content

Java 8 pop quiz

Juanitology

Here's a little quiz for those of you who've looked into Java 8. Given the following, what result would you expect when you attempt to compile?

import java.sql.SQLException;public class Oppgave3 {	public static void main(String[] args) {				new Oppgave3().foo();	}	public void foo(){		doThrow(new SQLException());	}	<E extends Exception>	void doThrow (Exception e) throws E{		throw (E) e;	}}

a) Compiler error in foo: unhandled exception

b) ClassCastException in doThrow: SQLException cannot be cast to RuntimeException

c) SQLException gets thrown out of foo

Corsair 600T | Intel Core i7-4770K @ 4.5GHz | Samsung SSD Evo 970 1TB | MS Windows 10 | Samsung CF791 34" | 16GB 1600 MHz Kingston DDR3 HyperX | ASUS Formula VI | Corsair H110  Corsair AX1200i | ASUS Strix Vega 56 8GB Internet http://beta.speedtest.net/result/4365368180

Link to comment
Share on other sites

Link to post
Share on other sites

Here's a little quiz for those of you who've looked into Java 8. Given the following, what result would you expect when you attempt to compile?

import java.sql.SQLException;public class Oppgave3 {	public static void main(String[] args) {				new Oppgave3().foo();	}	public void foo(){		doThrow(new SQLException());	}	<E extends Exception>	void doThrow (Exception e) throws E{		throw (E) e;	}}

a) Compiler error in foo: unhandled exception

B) ClassCastException in doThrow: SQLException cannot be cast to RuntimeException

c) SQLException gets thrown out of foo

idk about this stuff but i'm guessing a 

Link to comment
Share on other sites

Link to post
Share on other sites

I'll take "Not in school, I'd throw this into an IDE and have it print me a stack trace" for $1000, Alex.

 

There's no reason to be able to manually figure out what snippets like this are doing. That's what the compiler is for.

--Neil Hanlon

Operations Engineer

Link to comment
Share on other sites

Link to post
Share on other sites

I guess SqlException does not extend RuntimeException, so I bet it do A. I vaguely think that C is possible, but non runtime exceptions are checked, therefore have to be handled

Link to comment
Share on other sites

Link to post
Share on other sites

Ah...Java...

Primary: Lenovo T61 / Intel Core2Duo T7200 @ 2.2GHz / 3GB DDR2 / NVIDIA Quadro NVS 140M / Fedora 22 <<<< THE WHITE KNIGHT

Secondary: Compaq Presario CQ56 / AMD V130 @ 2.3GHz / 2GB DDR3 / AMD Radeon HD 4250 / Windows 8.1 <<< THE FORGOTTEN HERO

Link to comment
Share on other sites

Link to post
Share on other sites

I'll take "Not in school, I'd throw this into an IDE and have it print me a stack trace" for $1000, Alex.

 

There's no reason to be able to manually figure out what snippets like this are doing. That's what the compiler is for.

 

Y7cIRUh.jpg

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Exception in thread "main" java.sql.SQLExceptionat Oppgave3.foo(Oppgave3.java:14)at Oppgave3.main(Oppgave3.java:10)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:497)at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)Process finished with exit code 1

Imma gonna go with C there :)

 

See ya next time, Space Cowboy

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×