Questions tagged [throwable]

The `Throwable` class is the superclass of all errors and exceptions in the Java programming language. Only objects that are instances of this class (or one of its subclasses) are thrown by the Java VM or could be thrown by the Java `throw` statement.

The Throwable class is the superclass of all errors and exceptions in the Java programming language. Only objects that are instances of this class (or one of its subclasses) are thrown by the Java VM or could be thrown by the Java throw statement. Similarly, only this class or one of its subclasses can be the argument type in a catch clause. For the purposes of compile-time checking of exceptions, Throwable and any subclass of Throwable that is not also a subclass of either RuntimeException or Error are regarded as checked exceptions.

For the further details and reference the documentation for Class Throwable.

165 questions
445
votes
6 answers

Difference between using Throwable and Exception in a try catch

Sometimes, I see try { } catch(Throwable e) { } And sometimes try { } catch(Exception e) { } What is the difference?
jax
  • 37,735
  • 57
  • 182
  • 278
203
votes
11 answers

Differences between Exception and Error

I'm trying to learn more about basic Java and the different types of Throwables, can someone let me know the differences between Exceptions and Errors?
Marco Leung
  • 2,083
  • 3
  • 14
  • 9
117
votes
14 answers

Is it a bad practice to catch Throwable?

Is it a bad practice to catch Throwable? For example something like this: try { // Some code } catch(Throwable e) { // handle the exception } Is this a bad practice or we should be as specific as possible?
ktulinho
  • 3,870
  • 9
  • 28
  • 35
60
votes
8 answers

Exception handling : throw, throws and Throwable

Can any of you explain what the differences are between throw, throws and Throwable and when to use which?
Sumithra
  • 6,587
  • 19
  • 51
  • 50
48
votes
12 answers

When should Throwable be used instead of new Exception?

Given: Throwable is Exception's superclass. When I read texts on writing your own 'exceptions', I see examples of Throwable being used in the catch block and other texts show new Exception() being used in the catch block. I have yet to see an…
WolfmanDragon
  • 7,851
  • 14
  • 49
  • 61
48
votes
6 answers

Java - Throwable to Exception

I am currently using the play2 framework. I have several classes which are throwing exceptions but play2s global onError handler uses throwable instead of an exception. for example one of my classes is throwing a NoSessionException. Can I check a…
Maik Klein
  • 15,548
  • 27
  • 101
  • 197
36
votes
8 answers

JUnit @Test expected annotation not working

I've got the following test: @Test(expected = IllegalStateException.class) public void testKey() { int key = 1; this.finder(key); } But JUnit reports, that the test fails, although it throws — as expected — an IllegalStateException. Do I…
user321068
36
votes
8 answers

printStackTrace to java.util.logging.Logger

How do I print the entire stack trace using java.util.Logger? (without annoying Netbeans). The question should've originally specified staying within Java SE. Omitting that requirment was an error on my part. -do-compile: [mkdir] Created dir:…
Thufir
  • 8,216
  • 28
  • 125
  • 273
34
votes
8 answers

What is the preferred Throwable to use in a private utility class constructor?

Effective Java (Second Edition), Item 4, discusses using private constructors to enforce noninstantiability. Here's the code sample from the book: public final class UtilityClass { private UtilityClass() { throw new AssertionError(); …
Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
31
votes
7 answers

Extending Throwable in Java

Java lets you create an entirely new subtype of Throwable, e.g: public class FlyingPig extends Throwable { ... } Now, very rarely, I may do something like this: throw new FlyingPig("Oink!"); and of course elsewhere: try { ... } catch (FlyingPig…
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
27
votes
8 answers

Overhead associated with Exception vs Throwable in Java

I know throw new Exception(); has a pretty large overhead, since it creates a full stackTrace, etc. Does throw new Throwable(); present the same problem? Is this behaviour inherited, or does throwing a Throwable has a smaller (o no)…
WhyNotHugo
  • 9,423
  • 6
  • 62
  • 70
25
votes
2 answers

What is the C# equivalent to Java's Throwable?

What is the C# equivalent to Java's Throwable? In Java, the root of the exception class hierarchy is called Throwable, not Exception. The Throwable base class has two derived classes: Exception: for conditions that a reasonable application might…
24
votes
3 answers

NoClassDefFoundError at Runtime with Gradle

I'm using gradle as the JavaFX plugin. Everything works perfectly even after building and runnig the excecutable at distribution/, except with one class: CloseableHttpClient For several purposes I create the following object like…
jntme
  • 602
  • 1
  • 5
  • 18
24
votes
5 answers

Why doesn't Java support generic Throwables?

class Bouncy extends Throwable { } // Error: the generic class Bouncy may not subclass java.lang.Throwable Why doesn't Java support generic Throwables? I realize that type erasure complicates certain things, but obviously Java gets by…
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
19
votes
6 answers

How to not throw a generically specified exception?

I created a "producer" interface (to be used with method references, respectively to be easily mocked for unit tests): @FunctionalInterface public interface Factory { public R newInstanceFor(T t) throws X; } which I…
GhostCat
  • 137,827
  • 25
  • 176
  • 248
1
2 3
10 11