Java - Exception in 'throws' clause that is not being thrown from the body

exception handling in java javatpoint
throw exception java
java custom exception
java exception handling best practices
exception handling in java interview questions
uncaught exception in java
what is exception
user defined exception in java

I have a class like below:

public class MyClass {

    public void foo() throws IOException {
        System.out.println("Hola");
    }

    public MyClass() throws IOException {

    }

}

As you can see I am declaring IOException in the throws clause of the method and the constructor. But I am not throwing that exception anywhere in the body. So it should be a compile time error like when we try to catch an exception that is not being thrown from the try block. But in this case it compiles fine. Can anyone explain the reason behind this behavior?

Declaring throws IOException does not require that you actually throw the exception.

If that were the case, then it would be impossible to program because all branches of the method would be required to throw the exception (even in non-exceptional cases).

This is more a question of having complete contracts, where the caller is enabled to handled possible exceptions. This accommodates future implementations that may be forced to actually throw the exception. It's probably for the same reason that overridden methods are allowed to omit checked exceptions (you are not forced to throw it)

Java - Exceptions, The class Exception and any subclasses that are not also subclasses of RuntimeException are checked exceptions. Checked exceptions need to be declared in a  Java defines several exception classes inside the standard package java.lang. The most general of these exceptions are subclasses of the standard type RuntimeException. Since java.lang is implicitly imported into all Java programs, most exceptions derived from RuntimeException are automatically available.

Show Stopper has a great answer and references a great post. This answer is an attempt to help OP come to an understanding on what checked and unchecked exceptions are.

Checked exceptions are an exceptions declared in the signature (whether thrown or not).

public void doSomething() throws SomeExtremelyDevastatingEndOfTheWorldException {
    // Maybe it might happen (but whoever calls this MUST acknowledge handle this case)
}

This application will not compile because the author deemed it necessary that this case must be handled for the application to run.

Unchecked exceptions are exceptions that don't necessarily need to be handled and is acceptable to bubble up at runtime.

public void doSomething() {
    throws RuntimeException("not the end of the world...but we'll probably want this stacktrace and let the program run on");
}

Now anything exception can become an unchecked exception if the author wishes it to be.

Exception (Java Platform SE 7 ), An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions. The Catch or Specify Requirement. This section  Exceptions in Java What is an Exception? An exception is an unwanted or unexpected event, which occurs during the execution of a program i.e at run time, that disrupts the normal flow of the program’s instructions.

Declaring throws IOException does not require that method throw the exception.

It is part of method signature and also designing software. Consider below example. There is one method in Interface and two implementation of it. Both implementation should have same signature whether they are throwing exception or not. They can ignore exception clause. So this is valid reason why compiler can't show error while compilation.

Interface parent { 
    void method() throws Exception
}


Class Implementation1 {
    void method() throws Exception {
        System.out.println("Do not throws exception");
    }
}

Class Implementation2 {
    void method() throws Exception {
        throw new Exception("Error");
    }
}

Lesson: Exceptions (The Java™ Tutorials > Essential Classes), Errors happen all the time. Java provides a powerful exception handling mechanism that allows you to handle or propagate them. List of Java Exceptions. All public exceptions and errors in the Java API, grouped by package. : Checked exception <version>: Since version. Package java­.lang. Throwable Exception Clone­Not­Supported­Exception Interrupted­Exception Reflective­Operation­Exception Class­Not­Found­Exception Illegal­Access­Exception

Java Exception Handling: How to Specify and Handle Exceptions, The technical term for this is: Java will throw an exception (throw an error). Java try and catch. The try statement allows you to define a block of code to be tested for  What is Exception in Java. Dictionary Meaning: Exception is an abnormal condition. In Java, an exception is an event that disrupts the normal flow of the program. It is an object which is thrown at runtime. What is Exception Handling. Exception Handling is a mechanism to handle runtime errors such as ClassNotFoundException, IOException, SQLException, RemoteException, etc.

Java Exceptions (TryCatch), In Java, an exception is an event that disrupts the normal flow of the program. It is an object which is thrown at runtime. What is Exception Handling. Exception  An exception object is an instance of an exception class. It gets created and handed to the Java runtime when an exceptional event occurred that disrupted the normal flow of the application. This is called “to throw an exception” because in Java you use the keyword “throw” to hand the exception to the runtime.

Exception Handling in Java, An Exception is an unwanted event that interrupts the normal flow of the program. When an exception occurs program execution gets terminated. In such cases we​  Java exceptions cover almost all general exceptions that are bound to happen in programming. However, we sometimes need to supplement these standard exceptions with our own. The main reasons for introducing custom exceptions are: Business logic exceptions – Exceptions that are specific to the business logic and workflow.

Comments
  • Possible duplicate of Java: checked vs unchecked exception explanation