
how to Write a java program to throw a user defined exception if the student mark entered is less than fifteen

Here is a java program that throws an user-defined exception.
import java.util.*;
class MyException extends Exception{
String str;
MyException(String st) {
str=st;
}
public String toString(){
return ("Invalid Marks entered! "+ str) ;
}
}
class UserDefinedException{
public static void main(String args[]){
try{
Scanner input=new Scanner(System.in);
System.out.print("Enter marks: ");
int marks=input.nextInt();
if(marks < 15){
throw new MyException("Error! Marks are less than 15.");
}
}
catch(MyException e){
System.out.println(e) ;
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.