image
Exception Methods
Start course
Difficulty
Beginner
Duration
30m
Students
22
Ratings
5/5
Description

In this course, we will learn the concepts of microservice and spring framework and focus on Exception Handling.

Learning Objectives

  • Exception Handling in Java 

Intended Audience

  • Beginner Java developers
  • Java developers interested in learning how to Build and Deploy RESTful Web Services
  • Java Developers who want to develop web applications using the Spring framework
  • Java Developers who want to develop web applications with microservices
  • Java Developers who wish to develop Spring Boot Microservices with Spring Cloud

Prerequisites

  • Basic Java knowledge
Transcript

Hello there, my friends. In this video, we will talk about the useful methods in the Throwable class. Java exception and all its subclasses don't provide any specific methods. The Throwable class, which is the super class of the exception class, includes all of the methods. You see three useful methods of Throwable class in the slide. The first one is the getMessage() method. This method returns the string message of Throwable class. And this message can be provided while creating the exception through its constructor. This message is null if the exception object is created with no error message. The second one is the toString() method. This method returns a short description of Throwable. The third one is the printStackTrace() method. This method prints the stack trace information. Let's look at an example using the useful Throwable methods class. Right-click on the 'Exception' package and select new class. Specify the class name as ThrowableMethod and select the checkbox for the main method.

Outside the main method, let's declare a static double method divide with two double parameters: x and y. In this method, we check the value of variable y. If the value of variable y is zero, we throw ArithmeticException by using the throw keyword with no constructor message. If the value of variable y is not zero, we return x / y. Let's call this method inside the main method. In the main method, we declare a variable d with double type. We call the divide method with parameters 5.6 and 0 and assign this method to variable d. Let's enclose the code that may throw an exception with a try-catch block. In the catch block, we handle ArithmeticException. By using the print method, we display the ("Divider can not be equal to zero") message in this catch block. Okay, let's run the code. The catch block handles the exception and the ("Divider can not be equal to zero") message is displayed. Let's try to use useful methods of the Throwable class in the catch block. The first one is getMessage.

Let's write the getMessage() method in the print method for testing. This method returns the string message of the Throwable class. But notice that in the divide method, we didn't pass any message to the constructor of ArithmeticException class. So, this method returns null. Null is simply a value that indicates that the object reference is not currently referring to an object. Let's run the code. You see the result is null. In order to solve this problem, we need to pass a message to the constructor of ArithmeticException in the divide method. We pass the ("Arithmetic exception occurred") message. Okay, let's run the code again. Yes, you see the ("Arithmetic exception occurred") message in the console. The second method is toString. Let's write the toString() method in the print method for testing.

This method returns a short description of the Throwable class. All right, let's run the code. You see the type of exception with its package and its message. The last method is printStackTrace. This method prints the stack trace information and returns nothing. So, we can't use this method within the print method, because it's predefined with void, so we call it directly in the code. Okay, let's run the code again. You see the result? This method helps to trace the exception. By using this method, we can see which method causes the exception in which line number. In the console screen, if you choose the method that causes the exception, Eclipse automatically selects the line in the code. Apart from these, the getLocalizedMessage() method is one of the important methods used. If you want, let's use this as the last and end of the lesson. And as you can see, the error message  is briefly printed on the console screen. So, these are the useful methods of the Throwable class. Let's take a short break here. See you in the next lesson.

 

About the Author
Students
3984
Courses
64
Learning Paths
5

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.