throwable

Extending Throwable in Java

两盒软妹~` 提交于 2019-11-27 22:58:49
问题 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 been done to prevent this subtyping if it is a bad idea? Since it's not preventable (as far as I know), what catastrophies could result? If this isn't such a bad idea,

JUnit @Test expected annotation not working

空扰寡人 提交于 2019-11-27 21:50:59
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 have to configure something else to make this run? I run the test now with @RunWith(Suite.class) @SuiteClasses(Test.class) public class TestSuite { } like in this question , but am still not getting the desired result. And when I remove the test prefix I'm still getting an error. I gotta say that I run these tests with Eclipse, but it's configured to use the

Overhead associated with Exception vs Throwable in Java

随声附和 提交于 2019-11-27 19:42:32
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 analyst point of view, a user inserting wrong password is an exception to the normal execution order of a program. So if I have: public Session newSession() { validate_user_and_password(); } throwing a UserNotValidException would sound correct from an analysts point of view. Returning null or 0 just sounds incorrect if your code has pretty

Why doesn't Java support generic Throwables?

梦想的初衷 提交于 2019-11-27 13:38:22
问题 class Bouncy<T> extends Throwable { } // Error: the generic class Bouncy<T> may not subclass java.lang.Throwable Why doesn't Java support generic Throwable s? 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 Throwable s, with comprehensive compile-time check for potential problems? I feel like the type erasure argument is rather weak. Currently, we can't do: void process(List<String> list

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

孤者浪人 提交于 2019-11-27 13:33:20
问题 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 being "asserted", which is how the API defines the use of AssertionError. Is there a different Throwable that's typically in this situation? Does one usually just throw a general

NoClassDefFoundError at Runtime with Gradle

混江龙づ霸主 提交于 2019-11-27 04:37:22
问题 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 this: CloseableHttpClient client = HttpClients.createDefault(); Running the program in the IDE is no problem, everything works fine. But if I build and try to run the .exe-File I get the following Throwable -StackTrace: java.lang.NoClassDefFoundError: Could not

Overhead associated with Exception vs Throwable in Java

让人想犯罪 __ 提交于 2019-11-27 04:23:39
问题 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 analyst point of view, a user inserting wrong password is an exception to the normal execution order of a program. So if I have: public Session newSession() { validate_user_and_password(); } throwing a UserNotValidException would sound

When should Throwable be used instead of new Exception?

可紊 提交于 2019-11-26 22:12:37
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 explanation of when one should use each. My question is this, when should Throwable be used and when should new Exception() be used? Inside the catch or else block using either: throw throwable; or throw new Exception(); (from comments) The issue that brought this up is that I need to pass an 'exception' to a piece of code a coworker is building if a

JUnit @Test expected annotation not working

旧街凉风 提交于 2019-11-26 20:32:03
问题 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 have to configure something else to make this run? I run the test now with @RunWith(Suite.class) @SuiteClasses(Test.class) public class TestSuite { } like in this question, but am still not getting the desired result. And when I remove the test prefix I'm

Exception handling : throw, throws and Throwable

◇◆丶佛笑我妖孽 提交于 2019-11-26 15:12:55
Can any of you explain what the differences are between throw , throws and Throwable and when to use which? throws : Used when writing methods, to declare that the method in question throws the specified (checked) exception. As opposed to checked exceptions, runtime exceptions (NullPointerExceptions etc) may be thrown without having the method declare throws NullPointerException . throw : Instruction to actually throw the exception. (Or more specifically, the Throwable ). The throw keyword is followed by a reference to a Throwable (usually an exception). Example: Throwable : A class which you