-5

I want to throw an exception (any type) in Java, but the restriction is that i can't add " throws Exception " to my main method. So i tried this:

import java.io.IOException;

class Util
{
    @SuppressWarnings("unchecked")
    private static <T extends Throwable> void throwException(Throwable exception, Object dummy) throws T
    {
        throw (T) exception;
    }

    public static void throwException(Throwable exception)
    {
        Util.<RuntimeException>throwException(exception, null);
    }
}

public class Test
{
    public static void met() {
        Util.throwException(new IOException("This is an exception!"));  
    }

    public static void main(String[] args)
    {
        System.out.println("->main");
        try {
            Test.met();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

This code works, but when i am trying to catch an "IOException", for examle, in try-catch block, it doesnt compile. The compiler tells me that IOException is never thrown. It works only for exceptions that extend RuntimeException. Is there a way to solve this?

Added:

import java.io.IOException;

class Util
{
    @SuppressWarnings("unchecked")
    private static <T extends Throwable> void throwException(Throwable exception, Object dummy) throws T
    {
        throw (T) exception;
    }

    public static void throwException(Throwable exception)
    {
        Util.<RuntimeException>throwException(exception, null);
    }
}

public class Test
{
    public static void met() { // this method's signature can't be changed
        Util.throwException(new IOException("This is an exception!"));  
    }

    public static void main(String[] args)
    {
        System.out.println("->main");
        try {
            Test.met();
        } catch (IOException e) { // can't be changed and it does not compile right now
            System.out.println(e.getMessage());

        }
    }
}
Andrew
  • 63
  • 1
  • 9
  • 1
    Yes: use exceptions as they are supposed to be used, instead of cheating on the compiler with dirty tricks. If a method is throwing an IOException, it should declare it with `throws`. If you don't want `throws`, then use runtime exceptions. Or use a JVM language that chose not to have checked exceptions, like Kotlin. – JB Nizet Jan 10 '16 at 08:09
  • I am using this because i need it in a specific context where i can't change method signature... – Andrew Jan 10 '16 at 08:13
  • 3
    Then use a runtime exception, not a checked exception. – JB Nizet Jan 10 '16 at 08:13
  • I have the following situation: 1) i cannot change method signature 2) i have to make some already written "try -catch blocks" to work. – Andrew Jan 10 '16 at 08:14
  • What method signature can't you change? Could you give us a slightly more concrete example? – Makoto Jan 10 '16 at 08:19
  • 1
    Sorry, but that looks like a lazy excuse. Rewrite the code that needs to be rewritten properly: those catch blocks were obviously not meant to catch your new undeclared IOException in the first place, anyway. – JB Nizet Jan 10 '16 at 08:20
  • So you're *intentionally* catching an IOException, and you want to instead do...what, exactly? This is very confusing. – Makoto Jan 10 '16 at 08:27
  • This may help you http://stackoverflow.com/questions/4519557/is-there-a-way-to-throw-an-exception-without-adding-the-throws-declaration – Deepanshu Jan 10 '16 at 08:30
  • I want to catch any type of exception in Main (IOException / FileNotFoundException / etc) thrown by method "met()". The problem is that I can't change the signature of "met()", so i can't rewrite it to " met() throws ...". – Andrew Jan 10 '16 at 08:32

2 Answers2

3

The simple answer: you can't.

The more complex answer: you can't, and you really shouldn't look to do this. The main reason being, if your code can catch exceptions that it's not advertised to, then that leads to inconsistency and bugs.

Above all, that code block isn't meant to catch anything other than an IOException; that is, the code is only meant to recover on something going haywire with IO. If I were to try and catch anything else, then that would imply that the code knows how to recover from that scenario, which is very much not the case.

As an aside, any children of IOException will be caught by that block, so you don't have to worry about catching FileNotFoundExecption, since that will handle it.

Makoto
  • 104,088
  • 27
  • 192
  • 230
0

This is awful coding, and I feel dirty just writing it...

Instead of catch-ing the IOException directly, you can check that the caught Exception is an IOException.

public class Test
{
    public static void main(String[] args)
    {
        System.out.println("->main");
        try {
            Test.met();
        } catch (Exception e) {
            if (e instanceof IOException) {
                System.out.println(e.getMessage());
            }
        }
    }
}
WIlfinity
  • 905
  • 9
  • 10