"Hej, Amigo. Igår var jag lite sugen, så jag är rädd att jag har trasslat till dina uppgifter. Men jag hoppas att du inte blir arg på mig och kommer att slutföra dem alla. Det är för ditt eget bästa. Här komma."
3
Uppgift
Java Syntax, nivå, lektion
Exceptions. Just exceptions.
We have a method. It throws two exceptions that inherit Exception, and two exceptions that inherit RuntimeException: NullPointerException, ArithmeticException, FileNotFoundException, URISyntaxException. Task: catch NullPointerException and FileNotFoundException, without catching ArithmeticException or URISyntaxException. How do you do that?
3
Uppgift
Java Syntax, nivå, lektion
Catching a group of exceptions
There are 3 exceptions that successively inherit Exception (Exception1 extends Exception, Exception2 extends Exception1, and Exception3 extends Exception2). In the program, you will find the public static void method1() throws Exception1, Exception2, Exception3 method. Task: write a catch block that will catch all three exceptions: Exception1, Exception2, and Exception3.
6
Uppgift
Java Syntax, nivå, lektion
Catching custom exceptions
Perhaps you think multi-layered exception structures are evil and difficult. In reality, exceptions are simple and one of the most valuable parts of Java. This may be hard to believe on Level 9, but working with exceptions seems super confusing because it is still unfamiliar. Let's move forward without fear: in this task, you'll need to catch custom exceptions.
3
Uppgift
Java Syntax, nivå, lektion
Catching checked exceptions
Lessons at the secret CodeGym center have taught us that if a ClassNotFoundException or FileNotFoundException are thrown (occur) in a method, then the programmer is simply obligated to indicate them in the method signature. These are checked exceptions. Let's handle them in the handleExceptions method and display them on the screen.
3
Uppgift
Java Syntax, nivå, lektion
Catching unchecked exceptions
"But there's another kind of exception—the RuntimeException and the classes that inherit it. They don't have to be caught — because they're unchecked! They are a mystery. Their occurrence is hard to predict and they don't need to be indicated in the throws clause," say the CodeGym lessons. The time has come to handle all unchecked exceptions in the handleExceptions method.
3
Uppgift
Java Syntax, nivå, lektion
We're all friends here, even the exceptions
There was a time when exceptions kept young scientists and programmers awake at night. Today they have that effect on CodeGym students. Let's try creating our own exceptions. To do this, we have the MyException, MyException2, MyException3, and MyException4 classes. Let's have the classes inherit other classes so as to produce any two checked exceptions and any two unchecked exceptions.