Details Picture Team Photo Cowboys Football 8x10 Dallas About Nfl THE WORLD'S LARGEST WEB DEVELOPER SITE
Details Picture Team Photo Cowboys Football 8x10 Dallas About Nfl 1995 Details Picture Team Photo Cowboys Football 8x10 Dallas About Nfl 1995
1995 DALLAS COWBOYS. 8X10 PHOTO.Details about 1995 DALLAS COWBOYS 8X10 TEAM PHOTO FOOTBALL PICTURE NFL

Details Picture Team Photo Cowboys Football 8x10 Dallas About Nfl 1995


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
Details Picture Team Photo Cowboys Football 8x10 Dallas About Nfl }

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.");
    }
  }
}
Texas Just Apparel Tech Fans Smack - 2t-4t Tee anti-texas Me Toddler

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.Details Picture Team Photo Cowboys Football 8x10 Dallas About Nfl 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) {
    ifJulian Navy amp; Edelman Number New Name Receiver Majestic England Ii Patriots Eligible Men's Hoodie (age < 18) {
      throw new ArithmeticException("Access denied - You must be at least 18 years old.");
Details Picture Team Photo Cowboys Football 8x10 Dallas About Nfl
    }
    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

Desean Jersey Philadelphia Jackson Eagles

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



Panthers Jersey Military Panthers Military Jersey

Shirt Jersey Jersey Shirt Basketball