Answer the question
In order to leave comments, you need to log in
How to create your own exception class?
I'm trying to create a class with my own exceptions by type:
int num = x;
if (num == 0) {
throw new MyException("Это ноль!");
}
if (num<10){
System.out.println("Меньше 10");
}
else {
System.out.println("Больше");
}
class MyException extends Exception{
}
Answer the question
In order to leave comments, you need to log in
public class test10 {
public static void main(String[] args) throws Exception {
int num = x;
try {
if (num == 0) {
throw new MyException();
}
}
catch (MyException s){
System.out.println("Exception: " + s.toString());
return;
}
if (num<10){
System.out.println("Меньше 10");
}
else {
System.out.println("Больше");
}
}
static class MyException extends Exception{
public String toString() {
return "Недопустимое значение - ноль.";
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question