• Profile
  • Resume
  • Portfolio
  • Blog
Avatar

Nelson Silva

A human. Being. Coffee lover.

Blog

I Write My Thoughts Here

Exceptions in Java

March 14, 2022 Java

In this post, you will learn how to work with exceptions so that you can use that same knowledge and thus evolve.

  • an exception is an event that occurs during the execution of a piece of code that interrupts its normal flow of instructions;
  • in short, an exception represents an error;
  • the try executes the entire piece of code present in itself and automatically passes to the catch;
  • the catch checks the result of what was executed inside the try and transmits what is contained in it (most of the time the exception itself);
  • the finally is executed whether there is any exception or not.
package com.caffeinealgorithm.programminginjava;

public class Exceptions {
  public void Run() {
    int[] numbers = new int[] {
        1, 2, 3, 4, 5
    };

    try {
      System.out.printf("Element content: %d\n", numbers[4]);
    } catch (Exception exception) {
      // System.out.println("The code in the try is not working correctly.");
      System.out.println(exception);
    }
    finally {
      System.out.println("I am part of the finally and I'm executed if there is any exception or not.");
    }
  }
}

Don’t forget to watch the video and you can always read this post in Portuguese.

Happy coding!

Previous Next

Categories

  • Announcements
  • Python
  • C#
  • Java
  • Go

Latest Posts

  • for Loop in Go
    May 5, 2022
  • Switch in Go
    May 4, 2022
  • Comparison Operators in Go
    May 3, 2022
+351913416022
contact@nelsonsilvadev.com
Porto, Portugal
Nelson Silva © 2022 All Rights Reserved