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
4
votes
0 answers

Why does Java expose Error and Java SE API callback methods accept Throwable instead of Exception?

Facts The Java SE API says about the Error type that it "indicates serious problems that a reasonable application should not try to catch"; also "errors are abnormal conditions that should never occur". The JLS 11.1.3 "Asynchronous Exceptions" says…
4
votes
2 answers

JUnit TestWatcher : failed, is it possible to remove the thrown exception (manipulating Throwable/stacktrace)?

In JUnit, using a TestWatcher and Overriding the failed() function, is it possible to remove the thrown exception and instead make my own assertion? The use case is : with functional tests on Android, when a test makes the app crashes, I would like…
Fanch
  • 3,274
  • 3
  • 20
  • 51
4
votes
2 answers

Should I avoid throwing Throwable when dealing with a method that throws Throwable?

I've got an @Aspect annotated class that is calling ProceedingJoinPoint#proceed(). This method throws Throwable and thus the class looks something like this: @Aspect @Component public class MyClass{ @Around("@annotation(myAnnotation)") …
João Neves
  • 944
  • 1
  • 13
  • 18
4
votes
1 answer

Java Debugger runs application without breaking

I have the ubiquitous HelloWorldApp.java file /** * The HelloWorldApp class implements an application that * simply prints "Hello World!" to standard output. */ class HelloWorldApp { public static void main(String[] args) { …
mpdunson
  • 227
  • 1
  • 11
4
votes
2 answers

Which subclass of Throwable should be caught and which shouldn't?

API doc says never catch Throwable subclass Error which signifies abnormal behavior. Does it implies that the segregation between Error and Exception is to tell programmers that which subclass should be caught and which shouldn't ? Or there is more…
Ravi Gupta
  • 4,468
  • 12
  • 54
  • 85
4
votes
4 answers

Is it OK to catch Throwable for performing cleanup?

Take an example like this: public List readThings(List files) throws IOException { ImmutableList.Builder things = ImmutableList.builder(); try { for (File file : files) { …
Hakanai
  • 12,010
  • 10
  • 62
  • 132
3
votes
1 answer

Logging Throwable in main()

Catching Throwable is unadvisable for reasons outlined in different posts. However, would it make sense to have a main structured like below? If the Throwable line is removed, then errors would not be logged. public static void main(String[] args)…
assylias
  • 321,522
  • 82
  • 660
  • 783
3
votes
2 answers

What could be causing this Java uncaught error

I have a multithreaded Java program. The main thread executes the following code in a second thread, after which the second thread ends. try{ System.out.println(1); //prints doSomething(); System.out.println(2); //doesn't print }…
3
votes
4 answers

How to write an unchecked Throwable in java

I want to write a custom Throwable that is unchecked. There are ways to trick the compiler at throw time (e.g. Utility class that re-throws a throwable as unchecked?), which I have implemented thus: public class CustomThrowable extends Throwable { …
Gordon Bean
  • 4,272
  • 1
  • 32
  • 47
3
votes
1 answer

Catching throwables vs exceptions before calling CompletableFuture.completeExceptionally(Throwable)

I know the web is full of advice not to catch Throwables, but does it apply when working with CompleteableFutures? For example, in ExecutorService es = Executors.newFixedThreadPool(2); CompletableFuture cf = new…
3
votes
1 answer

Optional class in Android

I try to compile this code in android studio: public class Test { public void test() { java.util.Optional.of(12).orElseThrow(RuntimeException::new); } } It requires to handle Throwable. But signature of this method is next: public…
3
votes
1 answer

How to find out what the exact Throwable class is?

I have a try/catch block where I catch all Throwable exceptions. try { ... } catch (Throwable $ex) { ... } How do I, at runtime, figure out what the exact class of the throw exception is? I want to add multiple catch blocks to handle…
Rok Povsic
  • 4,626
  • 5
  • 37
  • 53
3
votes
3 answers

Is catching Java Throwable best way to restart thread

I have following code in java- @Override public void run() { logger.info("Thread Started:" + this.getName()); try { runJob(); } catch( Throwable e) { logger.error("Exception in running thread: " + this.getName() + ",…
thedevd
  • 683
  • 11
  • 26
3
votes
1 answer

Scala: idiomatic Try-match and avoiding catching Throwable

In Scala, a typical use of pattern matching for exception handling (at least per sources like this and this) looks like the following: Try(...) match { case Success(_) => println("success") case Failure(exc) => println(s"caught:…
sumitsu
  • 1,481
  • 1
  • 16
  • 33
3
votes
3 answers

Is it possible to throw a java exception through the calling method of a base class that does not throw exceptions?

This may be a ridiculous Java question about exception handling, but I have a UI actor (an Android Activity) that is requesting services from my subclass of ContentProvider. The subclass wants to throw some exceptions when sd-card is full, sd-card…
mobibob
  • 8,670
  • 20
  • 82
  • 131
1 2
3
10 11