I want to reproduce a part of InterruptedException behavior but I don't understand how it works...
So I have this code:
public static void main(String [] args){
try{
}catch(InterruptedException ie){
}
}
When I try to compile it I get this compiler error
Unreachable catch block for InterruptedException. This exception is never thrown from the try statement body
I made a custom Exception which is not really an Exception because it doesn't extend Exception...
class MyException extends Throwable{
}
public static void main(String [] args){
try{
}catch(MyException ie){
}
}
Which shows the same compiler error
Unreachable catch block for MyException. This exception is never thrown from the try statement body
Then I did this
public static void main(String [] args){
try{
throw new MyException();
} catch(MyException e){
e.printStackTrace();
}
try{
throw new InterruptedException();
} catch(InterruptedException e){
e.printStackTrace();
}
}
And both of them compile fine.
But now comes the tricky part..
public static void main(String [] args){
try{
throw new MyException();
} catch(Exception e){
e.printStackTrace();
} catch(MyException e){
e.printStackTrace();
}
try{
throw new InterruptedException();
} catch(Exception e){
e.printStackTrace();
} catch(InterruptedException e){
e.printStackTrace();
}
}
Compiler says
Unreachable catch block for InterruptedException. It is already handled by the catch block for Exception
Can you tell me how InterruptedException shows the "Unreachable catch block for InterruptedException. This exception is never thrown from the try statement body" compiler error and extends Exception in the same time, because when I extend exception my custom exceptions don't show this compiler error
As an example:
class MyException extends Exception{}
public static void main(String [] args){
try{
}catch(MyException me){
}
}
This code doesn't throw any compiler error
But the following code does
class MyException extends Throwable{}
public static void main(String [] args){
try{
}catch(MyException me){
}
}
This occurs because InterrupedException is a subclass of Exception, but Exception is already caught by the preceding catch block.
Section 11.2.3 of the JLS states:
It is a compile-time error if a catch clause can catch an exception class E1 and a preceding catch clause of the immediately enclosing try statement can catch E1 or a superclass of E1.
The block in the InterruptedException catch block would be unreachable code, which would be the justification for this compiler error.
You are chatching throwable exception so it has to be thrown from somewhere
by the way : my compiler shows error for 1st script and for 2nd script
Related
Everytime I run this code, everything works fine, but if deposit methods throws an error,
only the catch in the main method catches the exception and prints the string, despite the catch in the ExceptionsDemo. Why does that happen?
public class ExceptionsDemo {
public static void show() throws IOException {
var account = new Account();
try {
account.deposit(0);//this method might throw an IOException
account.withDraw(2);
} catch (InsufficientFundsException e) {
System.out.println(e.getMessage());
}
}
}
public class Main {
public static void main(String[] args) {
try {
ExceptionsDemo.show();
} catch (IOException e) {
System.out.println("An unexpected error occurred");
}
}
}
This happens because in your show() method, you are catching a specific type of exception called InsufficientFundsException. But the exception thrown by account.deposit() is IOException which is caught only in your main method. That is why the catch in the main method gets executed and not the catch in ExcpetionsDemo
If you want to catch IOException in your ExceptionsDemo class you can do this:
public static void show() {
var account = new Account();
try {
account.deposit(0);//this method might throw an IOException
account.withDraw(2);
} catch (InsufficientFundsException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
A single try block can have multiple catch blocks for each type of exception and the handling of each exception can be customized to what you want by coding it in each of their catch blocks.
Your ExceptionsDemo throws a IOException, your catch in ExceptionsDemo only catches a InsufficientFundsException so it will not be caught in the ExceptionsDemo, it will bubble to the caller, and be caught there, providing there is a catch block to handle said exception, which it does, otherwise you'll have an uncaught exception. It's not been rethrown from ExceptionsDemo, because its not being caught in the first place
try {
// do what you want
} catch (InsufficientFundsException e) {
// catch what you want
} catch (Exception e) {
// catch unexpected errors, if you want (optional)
}
class Demo
{
public static void main(String args[]) throws java.io.IOException
{
try(FileInputStream fin = new FileInputStream("Demo.txt"))
{
//This block is executed successfully
}
System.out.println("Will it be executed if error occurs in try clause");
}
}
Suppose that the code in try block is executed successfully as mentioned in the code, and some exception occurs in try with resource clause, that means exception occurs in auto closing of file.
How that exception in the try with resource clause will be handled?
What I want to ask is that will that exception be thrown to JVM and will terminate my program abruptly and the println statement will not be executed?
Can I catch that exception so that remaining program can also be executed?
If an exception is thrown by the close method of an AutoClosable it will indeed be thrown after the try block has been executed.
If you need to handle the exception you can simply add a catch clause to your try clause.
The following code illustrates the behavior:
public class Foo {
public static class Bar implements AutoCloseable {
#Override
public void close() {
throw new RuntimeException();
}
}
public static void main(String args[]) {
try (Bar b = new Bar()) {
// This block is executed successfully
}
System.out.println("Will it be executed if error occurs in try clause");
}
}
It terminates the JVM with the stack trace:
Exception in thread "main" java.lang.RuntimeException
at test3.Foo$Bar.close(Foo.java:14)
at test3.Foo.main(Foo.java:25)
25 is the line where the closing } for my try clause is.
It could be handled by using:
try (Bar b = new Bar()) {
// This block is executed successfully
} catch (Exception e) {
// ...
}
only add catch clause, to catch the exception otherwise program will be terminated
public static void main(String[] args) throws FileNotFoundException, IOException {
try(FileInputStream fin = new FileInputStream("Demo.txt"))
{
//This block is executed successfully
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println("Will it be executed if error occurs in try clause");
}
I think with a finally the rest of the program will be run, please try it and report.
class Demo
{
public static void main(String args[]) throws java.io.IOException
{
try(FileInputStream fin = new FileInputStream("Demo.txt"))
{
//This block is executed successfully
} finally {
System.out.println("Will it be executed if error occurs in try clause");
}
}
}
Just delete the file Demo.txt and run the following code.
The simplest way to throw such exception is to run this code, with no existing resource (Demo.txt in this case):
public static void main(String args[])
{
try(FileInputStream fin = new FileInputStream("Demo.txt"))
{
} catch(IOException exc) {
System.out.println("An exception has occured. Possibly, the file does not exist. " + exc);
}
System.out.println("Will it be executed if error occurs in try clause");
}
public class SampleCloseable implements AutoCloseable {
private String name;
public SampleCloseable(String name){
this.name = name;
}
#Override
public void close() throws Exception {
System.out.println("closing: " + this.name);
}
}
and the main class
public class Main{
public static void main(String args[]) {
try(SampleCloseable sampleCloseable = new SampleCloseable("test1")){
System.out.println("im in a try block");
} catch (IOException e) {
System.out.println("IOException is never thrown");
} catch (Exception e) {
} finally{
System.out.println("finally");
}
}
}
But when i removed the throws exception on close() method inside SampleCloseable
i am getting a compiler error saying that IOException is never thrown in the corresponding try block.
Because you're throwing a generic Exception. Since an IOException inherits from Exception, it might be thrown by the close() method. The caller doesn't know that it doesn't actually get thrown. It only sees the method signature that says that it could.
In fact, the close() method is free to throw any Exception of any kind. Of course that's bad practice, you should specify what specific Exceptions you're throwing.
Your confusion may that around the fact the in Java 7, the exception that is [declared to be] thrown from the close method is thrown inside the try block, so your catch block has to catch it as well.
Your close method is declared to throw an Exception, so your catch blocks have to catch that, or the method has to be declared to throw Exception.
And since IOException is a subclass of Exception, you are of course allowed to try and catch that as well, as long as your also catch/declare Exception itself.
See JLS 14.20.3.2:
The meaning of an extended try-with-resources statement [...] is given
by the following translation to a basic try-with-resources statement
(ยง14.20.3.1) nested inside a try-catch or try-finally or
try-catch-finally statement.
Your code is effectively translated to the below. Although a bit longish, it should be clear from the below what's happening in your code.
public static void main(String args[]) {
try {
Throwable primaryEx = null ;
SampleCloseable sampleCloseable = new SampleCloseable("test1")
try {
System.out.println("im in a try block");
} catch (Throwable t) {
primaryEx = t;
throw t;
} finally {
if (sampleCloseable != null) {
if (primaryEx != null) {
try {
sampleCloseable.close();
} catch (Throwable suppressedExc) {
primaryEx.addSuppressed(suppressedExc);
}
} else {
sampleCloseable.close();
}
}
} catch (IOException e) {
System.out.println("IOException is never thrown");
} catch (Exception e) {
} finally{
System.out.println("finally");
}
}
The main class:
class IO
{
static void m() throws Exception
{
try
{
throw new Exception();
} finally{
System.out.println("finally");
}
}
public static void main(String [] args)
{
try {
m();
} catch (Exception ex) {
System.out.println("catch");
}
System.out.println("finish");
}
}
Output:
finally
catch
finish
That behavior is unclear for me. Clause 11.3 of JLS 8 says:
If no catch clause that can handle an exception can be found, then the
current thread (the thread that encountered the exception) is
terminated. Before termination, all finally clauses are executed and
the uncaught exception is handled according to the following rules:
If the current thread has an uncaught exception handler set, then that
handler is executed.
Otherwise, the method uncaughtException is invoked for the ThreadGroup that is the parent > of the current thread. If the ThreadGroup and its parent ThreadGroups do not override
uncaughtException, then the default handler's uncaughtException method is invoked.
I expected that the output will be finally only because current thread is terminated. I haven't produced any other threads, since the output must be finally, but it is not true. Help me to understand, please.
If no catch clause that can handle an exception can be found, [...]
But you do have a catch clause that can handle the exception. Your m method will complete abruptly as a result of the exception being thrown. The exception will be caught and handled inside your main method which will then complete normally along with the main thread.
It might be easier to look at it like this, by replacing the function with the code from it.
class IO
{
public static void main(String [] args)
{
try { //4 Now goes to the outer try
try //2 Checks this try for the catch, but doesn't find it
{
throw new Exception(); //1 Hits the exception
} finally{ //3 Executes this because there is no catch for this try
System.out.println("finally");
}
} catch (Exception ex) { //5 Finds the catch
System.out.println("catch");
}
//6 Continues as if nothing happened
System.out.println("finish");
}
}
The following is the flow:
class IO
{
static void m() throws Exception
{
try
{ //2
throw new Exception();
} finally{
//3
System.out.println("finally");
}
}
public static void main(String [] args)
{
try {
m();//1 method called
} catch (Exception ex) {
//4 the control returns
System.out.println("catch");
}
//5
System.out.println("finish");
}
}
The thread is only terminated if the exception is not handled.
In Java, if a general exception is caught and rethrown, will outer methods still be able to catch specific exceptions?
In other words, can I do this:
try {
try {
//...
} catch (Exception e) {
//...
throw e;
}
} catch (SpecificException e) {
//...
}
re-throwing an exception does not change anything about it (it's still the same object originally thrown).
While jtahlborn answer is correct, there is one more appreciation: the compiler will see that you are throwing an exception of the generic type (even if at runtime it can be only of the specific class) and will force you to declare the generic exception in the method header.
private void test() throws FileNotFoundException {
try {
throw new FileNotFoundException("Es una exception");
} catch (IOException e) {
throw e; <-- Error because the method only throws
FileNotFoundException, not IOException
}
}
e is indeed FileNotFoundException, but as it is declared as IOException the compiler works with the broader class. What you can do is "cast" the exception.
throw (FileNotFoundException) e;
Eclipse marks the "throw e" in the inner catch as an unhandled exception, BUT it does catch the exception because when I run this it prints "It worked!". Thanks #jtahlborn. Unfortunately this means that there will still need to be an unnecessary try/catch block somewhere.
public class Tester {
public static void main(String[] args) {
try {
try {
throw new SpecificException("Test!");
} catch (Exception e) {
throw e;
}
} catch (SpecificException e) {
System.out.println("It worked!");
}
}
}