sadstephen
sadstephen
JCHJava Community | Help. Code. Learn.
Created by sadstephen on 1/26/2025 in #java-help
Why does creating a subclass of some Exception, throws this error?
Custom exceptions are supposed to be caught if occurs, not intentionally thrown.
jshell> class WrongInputException extends Exception {
...> public String toString() {
...> return "ERROR!!";
...> }
...> }

jshell> try {
...> ;
...> }
...> catch (ArithmeticException a) {
...> ;
...> } // <------- compiles fine here

jshell> try {
...> ; // if I use throw new WrongInputException() it would have compiled perfectly
...> }
...> catch (WrongInputException a) {
...> ;
...> }
| Error:
| exception WrongInputException is never thrown in body of corresponding try statement
| catch (WrongInputException a) {
| ^------------------------------...
jshell> class WrongInputException extends Exception {
...> public String toString() {
...> return "ERROR!!";
...> }
...> }

jshell> try {
...> ;
...> }
...> catch (ArithmeticException a) {
...> ;
...> } // <------- compiles fine here

jshell> try {
...> ; // if I use throw new WrongInputException() it would have compiled perfectly
...> }
...> catch (WrongInputException a) {
...> ;
...> }
| Error:
| exception WrongInputException is never thrown in body of corresponding try statement
| catch (WrongInputException a) {
| ^------------------------------...
9 replies
JCHJava Community | Help. Code. Learn.
Created by sadstephen on 1/19/2025 in #java-help
Can I make a superclass's variable static final in subclass?
abstract class User {
String about;
}
class Bots extend User {
final static String about = "Bots are not humans"; // I think this replaces Superclass 's instance variable about
}
abstract class User {
String about;
}
class Bots extend User {
final static String about = "Bots are not humans"; // I think this replaces Superclass 's instance variable about
}
21 replies