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
18
votes
3 answers

How to restrict that subclass cannot be generic?

Compile time error: The generic class may not subclass java.lang.Throwable public class TestGenericClass extends Exception { /*Above line will give compile error, the generic class TestGenericClass may not subclass java.lang.Throwable*/ …
Vishrant
  • 15,456
  • 11
  • 71
  • 120
17
votes
5 answers

If a NoClassDefFoundError is caused by a ClassNotFoundException, why does Java expect you to catch both throwables?

When I run this code the app exits with a ClassNotFoundException: //uncaught ClassNotFoundException try { Class clazz = defineClass(null, bytes, 0, bytes.length, null); table.put(clazz.getName(), clazz); } catch (NoClassDefFoundError…
J Smith
  • 2,375
  • 3
  • 18
  • 36
16
votes
4 answers

Difference between NoSuchMethodException and NoSuchMethodError in Java

I can't find exact difference between NoSuchMethodException and NoSuchMethodError in Java. Could someone give explanation and example of these two things ?
Balasubramani
  • 645
  • 3
  • 9
  • 16
13
votes
15 answers

Is there a favored idiom for mimicing Java's try/finally in C++?

Been doing Java for number of years so haven't been tracking C++. Has finally clause been added to C++ exception handling in the language definition? Is there a favored idiom that mimics Java's try/finally? Am also bothered that C++ doesn't have an…
RogerV
  • 3,826
  • 4
  • 28
  • 32
12
votes
4 answers

Best practices for catching Throwable in Java

Sometimes, you just have to catch Throwable, e.g. when writing a dispatcher queue that dispatches generic items and needs to recover from any errors (said dispatcher logs all caught exceptions, but silently, and then execution is continued on other…
ripper234
  • 222,824
  • 274
  • 634
  • 905
7
votes
2 answers

Throwable argument 'ex' to 'System.out.println()' call

I am currently working on a stock management system. In the system there is option to register as seen below: The following code shows the register button method: btnRegister.setOnAction(e ->{// register button method try { …
Hifitech
  • 125
  • 1
  • 8
7
votes
1 answer

Serialize fields in custom exception in Java

Let's say I have my custom RuntimeException, where MyEntity is JPA @Entity: @Getter public class MyEntityAlreadyExistingException extends RuntimeException { private final MyEntity myEntity; public MyEntityAlreadyExistingException(MyEntity…
Patrik Mihalčin
  • 3,341
  • 7
  • 33
  • 68
7
votes
6 answers

Get the error code from Throwable - Android

How can I get the error code from the Throwable? public void onFailure(Throwable exception) { } I saw that we can get the error messages, LocalizedMessage, etc.
Vineesh TP
  • 7,755
  • 12
  • 66
  • 130
6
votes
6 answers

Using Throwable for Things Other than Exceptions

I have always seen Throwable/Exception in the context of errors. But I can think of situations where it would be really nice to extend a Throwable just to break out of a stack of recursive method calls. Say, for example, you were trying to find and…
delmet
  • 1,013
  • 2
  • 9
  • 23
6
votes
2 answers

Throwable getCause returns null

I am catching a Throwable error. catch (Throwable t){ System.out.println(t.getCause().getMessage()); } When I put a breakpoint on a line System.out., and hover over (Eclipse) the t variable, Eclipse shows me the cause:…
yuris
  • 1,109
  • 4
  • 19
  • 33
6
votes
1 answer

I need an expert to make me understand what Java Throwable's addSuppressed does?

Possible Duplicate: JDK 1.7 Throwable `addSuppressed()` method So Java has a method in the Throwable public final void addSuppressed(Throwable exception) And this is what it does: Appends the specified exception to the exceptions that were…
Caffeinated
  • 11,982
  • 40
  • 122
  • 216
5
votes
1 answer

What's the difference between the next situations

Please, tell me difference between the next situations : public class Test { private static < T extends Throwable > void doThrow(Throwable ex) throws T { throw (T) ex; } public static void main(String[] args) { …
5
votes
1 answer

What is the difference between handled and un-handled exceptions

I'd like to know the difference between handled and unhandeled Exceptions in Java. which one i should use a try/catch block and when I have to extend throwables. also, if i extend throwables, do i always have to handle the exception in a separate…
Emad Abdelhamid
  • 305
  • 5
  • 16
5
votes
6 answers

Why does Exception.fillInStackTrace return Throwable?

I think Exception.fillInStackTrace should return Exception or derived Exception objects. Considering the two functions below, public static void f() throws Throwable { try { throw new Throwable(); } catch (Exception e) { …
Jichao
  • 40,341
  • 47
  • 125
  • 198
4
votes
2 answers

OpenJDK-java/lang/Throwable.fillInStackTrace - awkward implementation?

I am trying to understand the implementation of Throwable.fillInStackTrace(): private static final StackTraceElement[] UNASSIGNED_STACK = new StackTraceElement[0]; [...] private StackTraceElement[] stackTrace = UNASSIGNED_STACK; [...] /** * Fills…
Antonio Noack
  • 309
  • 3
  • 11
1
2
3
10 11