JetBrains Academy: Throwing exceptions - Kamil-Jankowski/Learning-JAVA GitHub Wiki

JetBrains Academy: Throwing exceptions

The root of a number:

Implement the sqrt method that counts the square root of a given number. If the negative number is passed the method should throw the java.lang.IllegalArgumentException with the message "Expected non-negative number, got ?", where ? is the passed negative number.

import java.util.*;

public class Main {

    public static double sqrt(double x) {
        // write your code here
        if (x < 0) {
            throw new IllegalArgumentException("Expected non-negative number, got " + x);
        }
        return Math.sqrt(x);
    }

    /* Do not change code below */
    public static void main(String[] args) {
        String value = new Scanner(System.in).nextLine();
        try {
            double res = sqrt(Double.parseDouble(value));
            System.out.println(res);
        } catch (IllegalArgumentException e) {
            System.out.println(e.getMessage());
        }
    }
}

Throw an unchecked exception:

Modify the given method. It should throw an unchecked exception.

public class Main {

    public static void method() {
        // write your code here
        throw new RuntimeException();
    }

    /* Do not change code below */
    public static void main(String[] args) {
        try {
            method();
        } catch (RuntimeException e) {
            System.out.println("RuntimeException");
        } catch (Exception e) {
            System.out.println("Exception");
        }
    }
}

Throw a checked exception:

Modify the given method. It should throw a checked exception.

public class Main {

    // modify given method
    public static void method() throws Exception {
        throw new Exception();
    }

    /* Do not change code below */
    public static void main(String[] args) {
        try {
            method();
        } catch (RuntimeException e) {
            System.out.println("RuntimeException");
        } catch (Exception e) {
            System.out.println("Exception");
        }
    }
}

Throw an IOException:

Modify the given method. It should throw an IOException.

import java.io.IOException;

public class Main {

    // change this method
    public static void method() throws IOException {
        throw new IOException();
    }

    /* Do not change code below */
    public static void main(String[] args) {
        try {
            method();
        } catch (Exception e) {
            System.out.println(e.getClass());
        }
    }
}

Days of the week:

Your task is to implement the getDayOfWeekName method that converts the number of the day of the week to its short name. If the given number is incorrect, the method should throw IllegalArgumentException.

Let's assume that a week starts from Monday:

  • 1 → "Mon";
  • 2 → "Tue";
  • 3 → "Wed";
  • 4 → "Thu";
  • 5 → "Fri";
  • 6 → "Sat";
  • 7 → "Sun".
import java.util.*;

public class Main {

    public static String getDayOfWeekName(int number) {
        // write your code here
        switch (number) {
            case 1:
                return "Mon";
            case 2:
                return "Tue";
            case 3:
                return "Wed";
            case 4:
                return "Thu";
            case 5:
                return "Fri";
            case 6:
                return "Sat";
            case 7:
                return "Sun";
            default:
                throw new IllegalArgumentException();
        }
    }

    /* Do not change code below */
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int dayNumber = scanner.nextInt();
        try {
            System.out.println(getDayOfWeekName(dayNumber));
        } catch (Exception e) {
            System.out.println(e.getClass().getName());
        }
    }
}