checked-exceptions

Isn't an unchecked exception that is caught in a try block a checked exception in Java?

房东的猫 提交于 2019-11-30 09:15:47
I was told that in Java, unchecked exceptions can be caught in a try block, but if it's caught, isn't it called a checked exception? Unchecked exceptions are exceptions that don't need to be caught in a try - catch block. Unchecked exceptions are subclasses of the RuntimeException or Error classes. Checked exceptions are exceptions that need to be caught in a try - catch block. The definition of checked and unchecked exceptions can be found in Section 11.2: Compile-Time Checking of Exceptions of The Java Language Specification : The unchecked exceptions classes are the class RuntimeException

Guava cache and preserving checked exceptions

点点圈 提交于 2019-11-30 03:04:57
I'm refactoring some code to use guava Cache . Initial code: public Post getPost(Integer key) throws SQLException, IOException { return PostsDB.findPostByID(key); } In order not to break something I need to preserve any thrown exception as is, without wrapping it. Current solution appears somewhat ugly: public Post getPost(final Integer key) throws SQLException, IOException { try { return cache.get(key, new Callable<Post>() { @Override public Post call() throws Exception { return PostsDB.findPostByID(key); } }); } catch (ExecutionException e) { Throwable cause = e.getCause(); if (cause

Isn't an unchecked exception that is caught in a try block a checked exception in Java?

巧了我就是萌 提交于 2019-11-29 14:15:11
问题 I was told that in Java, unchecked exceptions can be caught in a try block, but if it's caught, isn't it called a checked exception? 回答1: Unchecked exceptions are exceptions that don't need to be caught in a try - catch block. Unchecked exceptions are subclasses of the RuntimeException or Error classes. Checked exceptions are exceptions that need to be caught in a try - catch block. The definition of checked and unchecked exceptions can be found in Section 11.2: Compile-Time Checking of

Checked exceptions thrown from within lambda expressions

自作多情 提交于 2019-11-29 07:24:43
Can you please explain why checked exceptions have to be caught from within lambda expressions? In other words, why does the following code not compile... public void doSomething(ObjectInputStream istream) throws IOException { // The read method throws an IOException. IntStream.range(0, 10).forEach(i -> someList.add(read(istream))); } but this one will? public void doSomething(ObjectInputStream istream) throws IOException { IntStream.range(0, 10).forEach(i -> { try { // The read method throws an IOException. someList.add(read(istream)); } catch (IOException ioe) { // Callee has to handle

How to wrap checked exceptions but keep the original runtime exceptions in Java

♀尐吖头ヾ 提交于 2019-11-29 05:28:21
I have some code that might throw both checked and runtime exceptions. I'd like to catch the checked exception and wrap it with a runtime exception. But if a RuntimeException is thrown, I don't have to wrap it as it's already a runtime exception. The solution I have has a bit overhead and isn't "neat": try { // some code that can throw both checked and runtime exception } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } Any idea for a more elegant way? I use a "blind" rethrow to pass up checked exceptions. I have used this for passing through the

Guava cache and preserving checked exceptions

♀尐吖头ヾ 提交于 2019-11-29 00:47:10
问题 I'm refactoring some code to use guava Cache. Initial code: public Post getPost(Integer key) throws SQLException, IOException { return PostsDB.findPostByID(key); } In order not to break something I need to preserve any thrown exception as is, without wrapping it. Current solution appears somewhat ugly: public Post getPost(final Integer key) throws SQLException, IOException { try { return cache.get(key, new Callable<Post>() { @Override public Post call() throws Exception { return PostsDB

Throwing exception from lambda [duplicate]

限于喜欢 提交于 2019-11-29 00:16:42
This question already has an answer here: Java 8 Lambda function that throws exception? 24 answers Given this java 8 code public Server send(String message) { sessions.parallelStream() .map(Session::getBasicRemote) .forEach(basic -> { try { basic.sendText(message); } catch (IOException e) { e.printStackTrace(); } }); return this; } how do we properly make this IOException be delegated up the stack of the method call? (in nutshell how to make this method throw this IOException ?) Lambdas in java does not look very friendly to error handling... Marko Topolnik My approach would be to sneakily

Why is catching checked exceptions allowed for code that does not throw exceptions?

夙愿已清 提交于 2019-11-28 18:12:53
In Java, methods that throw checked exceptions ( Exception or its subtypes - IOException, InterruptedException, etc) must declare throws statement: public abstract int read() throws IOException; Methods that do not declare throws statement can't throw checked exceptions. public int read() { // does not compile throw new IOException(); } // Error: unreported exception java.io.IOException; must be caught or declared to be thrown But catching checked exceptions in safe methods is still legal in java: public void safeMethod() { System.out.println("I'm safe"); } public void test() { // method

Checked vs. Unchecked Exceptions in Service Layer

只谈情不闲聊 提交于 2019-11-28 17:37:30
I work on a project with a legacy service layer that returns null in many places if a requested record does not exist, or cannot be accessed due to the caller not being authorized. I am talking about specific records requested by ID. For instance, something like: UserService.get(userId); I have recently pushed to have this API changed, or supplemented with a new API that throws exceptions instead. The debate over checked vs unchecked exceptions has ensued. Taking a note from the designers of JPA/Hibernate et all., I have suggested that unchecked exceptions may be most appropriate. My argument

Wrapping a checked exception into an unchecked exception in Java?

a 夏天 提交于 2019-11-28 04:42:12
I have this factory method in java: public static Properties getConfigFactory() throws ClassNotFoundException, IOException { if (config == null) { InputStream in = Class.forName(PACKAGE_NAME).getResourceAsStream(CONFIG_PROP); config = new Properties(); config.load(in); } return config; } And I want to transform the two checked exceptions into unchecked exceptions. What is the best way to go about this? Should I just catch the exception and throw a new RuntimeException using the caught exception as the inner exception? Is there a better way to do this or should I even be attempting to do this