PERTEMUAN KE –12
Exception
pratik 1
import java.util.Scanner;
public class TestError {
public static void main(String[] arg){
Scanner al = new Scanner(System.in);
try {
System.out.print("Please enter the miles you have driven : ");
double miles = al.nextDouble();
System.out.print("Please enter the gallon of gas you have used : ");
double gallon = al.nextDouble();
double mpg = miles/gallon;
System.out.println("Your car's mpg is " + mpg + ".");
}
catch (Exception e){
System.out.println("You must enter a number.");
}
}
}
pratik 2
import java.util.Scanner;
public class TestError2 {
public static void main(String[] arg){
Scanner al = new Scanner(System.in);
try {
System.out.print("Please enter the miles you have driven : ");
double miles = al.nextDouble();
System.out.print("Please enter the gallon of gas you have used : ");
double gallon = al.nextDouble();
if (gallon ==0)
throw new ArithmeticException();
double mpg = miles/gallon;
System.out.println("Your car's mpg is " + mpg + ".");
}
catch (Exception e){
System.out.println("You must enter a number.");
System.out.println(e.getClass());
}
}
}
pratik 3
import java.util.Scanner;
public class TestError3 {
public static void main(String[] arg){
Scanner al = new Scanner(System.in);
try {
System.out.print("Please enter the miles you have driven : ");
double miles = al.nextDouble();
System.out.print("Please enter the gallon of gas you have used : ");
double gallon = al.nextDouble();
if (gallon ==0)
throw new ArithmeticException();
double mpg = miles/gallon;
System.out.println("Your car's mpg is " + mpg + ".");
}catch (ArithmeticException e){
System.out.println("You can not divide by zero");
}catch (Exception e){
System.out.println("You must enter a number.");
}
}
}
import java.util.Scanner;
public class TestError5 {
public static void main(String[] arg){
Scanner reader = new Scanner(System.in);
try {
System.out.print("Please enter the miles you have driven : ");
double miles = reader.nextDouble();
System.out.print("Please enter the gallon of gas you have used : ");
double gallon = reader.nextDouble();
if (gallon ==0)
throw new DivideByZeroException1();
if (gallon<0 || miles <0)
throw new NegativeNumberException1();
double mpg = miles/gallon;
System.out.println("Your car's mpg is " + mpg + ".");
}catch (DivideByZeroException1 e){
System.out.println("You can not divide by zero");
}catch (NegativeNumberException1 e){
System.out.println("You can not use negatif numbers");
}catch (Exception e){
System.out.println("You must enter a number.");
System.out.println(e.getClass());
}
}
}
class DivideByZeroException1 extends Exception{
DivideByZeroException1(){
}DivideByZeroException1(String message){
super(message);
}
}class NegativeNumberException1 extends Exception{
NegativeNumberException1(){
}
NegativeNumberException1(String message){
super(message);
}
}
pratik 5
import java.util.Scanner;
public class TestError6 {
public static void main(String[] arg){
Scanner reader = new Scanner(System.in);
try {
System.out.print("Please enter the miles you have driven : ");
double miles = reader.nextDouble();
System.out.print("Please enter the gallon of gas you have used : ");
double gallon = reader.nextDouble();
if (gallon ==0)
throw new DivideByZeroException1();
if (gallon<0 || miles <0)
throw new NegativeNumberException1();
double mpg = miles/gallon;
System.out.println("Your car's mpg is " + mpg + ".");
}catch (DivideByZeroException1 e){
System.out.println("You can not divide by zero");
}catch (NegativeNumberException1 e){
System.out.println("You can not use negatif numbers");
} catch (Exception e){
System.out.println("You must enter a number.");
System.out.println(e.getClass());
}finally {
System.out.println("Thank you for using the MPG System");
}
System.out.println("Program ends");
}
}








