throwable

Java: 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(); } } However, AssertionError doesn't seem like the right thing to throw. Nothing is bei...

When should Throwable be used instead of new Exception?

Given: Throwable is Exception's superclass. When I read tests on writing your own 'exceptions', I see examples of Throwable being used in the catch block and other text's show new Exception() being used in the catch block. I have yet to see an explanation of when one should use each. My question is this, When should Throwable be used an...

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 ultimate super type for all possible exceptions that could be thrown - like Java's Throw...

Differences betweeen 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? ...

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 items). One best practice I can think of is to always rethrow the exception if it's Inter...

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) { System.out.println("catch exception e"); e.printStackTrace(); } } public static ...

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) overhead? EDIT From an * annalist* point of view, a user inserting wrong password is an exception ...

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 to it ? ...

Why doesn't Java support generic Throwables?

class Bouncy<T> extends Throwable { } // Error: the generic class Bouncy<T> 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 with a lot already, so why not push it one more notch and allow generic Throwables, with c...

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 porky) { ... } My questions are: Is this a bad idea? And if so, why? What could've...

good documentation about "avoid catching throwable", in context of weblogic server

hi all, i am currently refactoring an existing codebase (EJBs...) to rip out all blocks where a Throwable is catched inside of the EJB. try { ... do some business logic } catch(Throwable t){ ... log and swallow ... :-( } i want/need to convince the people around me with proper documentation that "catching throwable" is a no-g...

Question about Java.lang.Error

There are lot of posts on java.lang.Error saying it should not be caught. My question is if it should not be caugth the what is the use of it. Since it is Throwable so we can catch it in try catch. I read some posts like only in some situation it should be caught, how to know these situations. In short i want to know what can go wrong w...

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 is missing, network i/o errros, etc. However, when I code the CP-subclass to throw my exc...