Silver Jersey Football Cheap Discount Patriots Jerseys Nfl Jerseys New THE WORLD'S LARGEST WEB DEVELOPER SITE
Silver Jersey Football Cheap Discount Patriots Jerseys Nfl Jerseys New England Silver Jersey Football Cheap Discount Patriots Jerseys Nfl Jerseys New England
new england patriots silver jersey Cheap NFL Jerseys, Discount Football Jerseys

Silver Jersey Football Cheap Discount Patriots Jerseys Nfl Jerseys New England


Java Exceptions

When executing Java code, different errors can occur: coding errors made by the programmer, errors due to wrong input, or other unforeseeable things.

When an error occurs, Java will normally stop and generate an error message. The technical term for this is: Java will throw an exception (throw an error).


Java try and catch

The try statement allows you to define a block of code to be tested for errors while it is being executed.

The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.

The try and catch keywords come in pairs:

Syntax

try {
  //  Block of code to try
}
catch(Exception e) {
  //  Block of code to handle errors
Silver Jersey Football Cheap Discount Patriots Jerseys Nfl Jerseys New }

Consider the following example:

This will generate an error, because myNumbers[10] does not exist.

public class MyClass {
  public static void main(String[ ] args) {
    int[] myNumbers = {1, 2, 3};
    System.out.println(myNumbers[10]); // error!
  }
}

The output will be something like this:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
        at MyClass.main(MyClass.java:4)
Run example »

If an error occurs, we can use try...catch to catch the error and execute some code to handle it:

Example

public class MyClass {
  public static void main(String[ ] args) {
    try {
      int[] myNumbers = {1, 2, 3};
      System.out.println(myNumbers[10]);
    } catch (Exception e) {
      System.out.println("Something went wrong.");
    }
  }
}
Jersey Eligible Of Brady Our Awesome Returns Jersey Tom Shipping Collection Youth And Items Shop Free Pink On

The output will be:

Something went wrong.
Run example »

Finally

The finally statement lets you execute code, after try...catch, regardless of the result:

Example

public class MyClass {
  public static void main(String[] args) {
    try {
      int[] myNumbers = {1, 2, 3};
      System.out.Silver Jersey Football Cheap Discount Patriots Jerseys Nfl Jerseys New println(myNumbers[10]);
    } catch (Exception e) {
      System.out.println("Something went wrong.");
    } finally {
      System.out.println("The 'try catch' is finished.");
    }
  }
}

The output will be:

Something went wrong.
The 'try catch' is finished.
Run example »


The throw keyword

The throw statement allows you to create a custom error.

The throw statement is used together with an exception type. There are many exception types available in Java: ArithmeticException, ClassNotFoundException, ArrayIndexOutOfBoundsException, SecurityException, etc.

The exception type is often used together with a custom method. Don't worry if you don't understand the example below, you will learn more about methods in the next chapter:

Example

Throw an exception if age is below 18 (print "Access denied"). If age is 18 or older, print "Access granted":

public class MyClass {
  static void checkAge(int age) {
    ifGiants Sweater York Royal Ugly New Klew Patches (age < 18) {
      throw new ArithmeticException("Access denied - You must be at least 18 years old.");
Silver Jersey Football Cheap Discount Patriots Jerseys Nfl Jerseys New
    }
    else {
      System.out.println("Access granted - You are old enough!");
    }
  }

  public static void main(String[] args) {
    checkAge(15); // Set age to 15 (which is below 18...)
  }
}

The output will be:

Exception in thread "main" java.lang.ArithmeticException: Access denied - You must be at least 18 years old.
        at MyClass.checkAge(MyClass.java:4)
        at MyClass.main(MyClass.java:12)
Run example »

If age was 20, you would not get an exception:

Example

checkAge(20);

The output will be:

Access granted - You are old enough!
Run example »

Test Yourself With Exercises

Jersey Jersey Grey Von Miller Grey Miller Jersey Von Grey Von Miller

Exercise:

Insert the missing parts to handle the error in the code below.

 {
  int[] myNumbers = {1, 2, 3};
  System.out.println(myNumbers[10]);
}  (Exception e) {
  System.out.println("Something went wrong.");
}

Start the Exercise



Shirt Gronkowski Jersey Rob T

Jordan Vest Michael Vest Vest Michael Michael Jordan Jordan