Näide

import java.io.*;

public class Erind {

public static void main (String[] args) {
int n = 0;
int m = 0;
while (true) {
try {
BufferedReader sisse = new BufferedReader
(new InputStreamReader (System.in));
System.out.println ("V2ljumiseks vajutage ctrl-C");
System.out.print ("Laste arv: ");
String s = sisse.readLine();
n = Integer.parseInt (s);
if (n<0)
throw new IllegalArgumentException (String.valueOf (n));
System.out.print ("6unte arv: ");
s = sisse.readLine();
m = Integer.parseInt (s);
if (m<0)
throw new IllegalArgumentException (String.valueOf (m));
System.out.println ("Igale lapsele "
+ String.valueOf (m/n) + " 6una ja "
+ String.valueOf (m%n) + " j22b yle");
}
catch (ArithmeticException e) {
System.out.println ("Aritmeetikakatkestus: " + e.toString());
}
catch (Exception muu) {
System.out.println ("Probleem: " + muu.toString());
}
finally { // see t2idetakse igal juhul
System.out.println ("n = " + n + " m = " + m);
}
} // while
} // main

} // Erind




/** A small example about catching NumberFormatException.
* @author Jaanus Poial
* @version 0.2
*/
public class ExceptionUsage {

/** Main method gets an integer parameter from the command line.
* If the parameter is not a number or if it is not in between 1 and 99
* an IllegalArgumentException is thrown.
*/
public static void main (String[] args) {
int n = 0;
try {
n = Integer.parseInt (args[0]); // throws NumberFormatException
} catch (Exception e) {
// n remains zero and a new exception is thrown
}
if (n<1 || n>99)
throw new IllegalArgumentException
("\nCommand line parameter has to be a number in between 1 and 99 ");
System.out.println("Parameter n = " + String.valueOf (n));
} // main

} // ExceptionUsage

 


Jaanus Pöial