0

When I try to write this code :

catch (Exception  | OutOfMemoryError| NumberFormatException| SQLException| IOException  e){
}

I get the error: "The exception NumberFormatException is already caught by the alternative Exception" I understand it great and from first thinking it's make sense because Exception is the general one. but when I write this code:

 catch (Exception  | OutOfMemoryError  e){
    }

It works without any problems so now I start to get confused why don't I get the previous error. Is OutOfMemmoryError will catch things that Exception won't catch? If I Want to make catch that will include all of exceptions and errors possible what should i write? I just understood that this:

catch (Exception e){
}

won't catch for me all the exceptions and errors so what will do it?

user3100708
  • 148
  • 3
  • 19
  • possible duplicate of [How can I catch all the exceptions that will be thrown through reading and writing a file?](http://stackoverflow.com/questions/1075895/how-can-i-catch-all-the-exceptions-that-will-be-thrown-through-reading-and-writi) – Marv Dec 17 '14 at 17:23
  • You should look at the inheritance hierarchy of `Throwable`. – Sotirios Delimanolis Dec 17 '14 at 17:23
  • 1
    OutOfMemeoryError is of type java.lang.Error, not Throwable.. – stan Dec 17 '14 at 17:24
  • 2
    @StanislavPalatnik [Error is Throwable](http://docs.oracle.com/javase/7/docs/api/java/lang/Error.html) – amit Dec 17 '14 at 17:25
  • @FastSnail what does || meaning? when i tried it now it gave me syntax error and i am not trying just to solve the error. i want to understand the logic of all this exceptions and create catch block that will catch all error and exceptions – user3100708 Dec 17 '14 at 17:26
  • @user3100708 That comment was just wrong and it's been deleted. `||` is not correct syntax. – ajb Dec 17 '14 at 17:26
  • @StanislavPalatnik so how do you think i can catch all the exceptions and error in one catch block? – user3100708 Dec 17 '14 at 17:28
  • 1
    @amit: Meant `Exception`. – stan Dec 17 '14 at 17:28
  • See http://stackoverflow.com/questions/2679330/catching-java-lang-outofmemoryerror – Raedwald Apr 02 '16 at 14:45

5 Answers5

3

The code catch (Exception | OutOfMemoryError e){ } compiles because OutOfMemoryError is not an Exception, so Exception doesn't cover OutOfMemoryError. OutOfMemoryError is an Error and it is not an Exception.

It's usually not a good idea to catch Errors, because they are usually unrecoverable anyway.

rgettman
  • 176,041
  • 30
  • 275
  • 357
3

To better understand what's happening, take a look at the Java exception hierarchy. A broad overview can be seen in this diagram.

When you catch multiple exceptions in a single catch block, you should only use the "topmost" exceptions in catch. For example, your error will persist in something like

catch (IOException | FileNotFoundException e)

since FileNotFoundException extends IOException.

In your code, all exceptions (except OutOfMemoryError) are subtypes of java.lang.Exception, hence the error. OutOfMemoryError is a subtype of java.lang.Error, which is a Throwable, but not an Error.

Here is a more fine-grained diagram representing the hierarchy of all java.lang.Exceptions within Throwable.

Hope this helped.

Finally, if you really want to catch all exceptions and errors in one catch block (a terrible idea, as others have pointed out), you could do one of the following:

catch (Exception | Error e)

or

catch (Throwable t)
Chthonic Project
  • 8,216
  • 1
  • 43
  • 92
2

You can catch Throwable, which is a superclass of everything, that can be thrown, errors or exceptions.

As pointed out previously though, this is not a good idea, and not something you should do. You should only be catching those exceptions from which you can meaningfully recover.

Dima
  • 39,570
  • 6
  • 44
  • 70
  • The purpose of my program is different than regular program. it should just run and when error appears start again(making a lot of network connection, so there are so many optional errors) so catch all the errors and exceptions is great solution for me. – user3100708 Dec 17 '14 at 17:41
  • If you are out of memory, or, perhaps, getting killed by OS because of some kind of resource restriction, or have a bug in the code, that just makes it die every time, catching and ignoring those errors isn't going to do you much good whether your program is "regular" or not. – Dima Dec 17 '14 at 17:47
  • I get this OutofMemoryException each 1-2 hours that my prog runs. If I run the program again immediately after i got this error it works again great for 1-2 hours. so why should i run the program by myself each 1-2 hours if i can write on the catch to start again? – user3100708 Dec 17 '14 at 17:50
  • Note that i can't create less objects in my code and i did my code considering hardly this error but its the best solution i got. – user3100708 Dec 17 '14 at 17:51
  • You don't need to catch the exception to start it again. Just let it exist and restart – Dima Dec 17 '14 at 17:55
  • what do you mean? which functions should i use? can you give me code example? i want it to restart automatically – user3100708 Dec 17 '14 at 18:01
  • no, I mean, outside of java. Just write a script, that keeps restarting your program ... For the record, I still think it is wrong (you are being lazy, and hacking a work around rather than debug your program and find a real solution) – Dima Dec 17 '14 at 18:54
1

It is not allowed to specify two or more exceptions of a same hierarchy in the multi-catch statement.

  • Yes i understood that but still i can't understand how to create catch block that will catch all error or exception my program will get – user3100708 Dec 17 '14 at 17:31
1

If you want to catch 'everything' you can catch (Throwable t) which which catch errors, exceptions, runtimeexceptions, etc... though I would recommend catching individual exceptions as you would likely want your app to handle different types of Exceptions/Errors/ differently.

cmparish
  • 66
  • 3
  • Ok, and what the difference between doing this (Throwable t) to catch(Exception | Error e) or it's actually the same? – user3100708 Dec 17 '14 at 17:37
  • @user3100708 In theory, someone could create a class that directly extended `Throwable` and *not* `Exception` or `Error` but that would be an (ahem) *exceptionally* bad idea. – David Conrad Dec 17 '14 at 17:42