1public static void foo() throws IOException {
2 // some code here, when something goes wrong, you might do:
3 throw new IOException("error message");
4}
5
6public static void main(String[] args) {
7 try {
8 foo();
9 } catch (IOException e) {
10 System.out.println(e.getMessage());
11 }
12}
1throw new java.lang.Error("this is very bad");
2throw new java.lang.RuntimeException("this is not quite as bad");
1public static void main(String[] args) {
2 Scanner kb = new Scanner(System.in);
3 System.out.println("Enter a number");
4 try {
5 double nb1 = kb.nextDouble();
6 if(nb1<0)
7 throw new ArithmeticException();
8 else System.out.println( "result : " + Math.sqrt(nb1) );
9 } catch (ArithmeticException e) {
10 System.out.println("You tried an impossible sqrt");
11 }
12}