Exception Methods

Start course
Overview
Difficulty
Beginner
Duration
50m
Students
24
Description

In this course, we'll learn about Exception Handling in Java.

Learning Objectives

  • What is an Exception?
  • Difference between Error and Exception
  • Types of Exceptions
  • Try-Catch Block
  • Finally Block
  • Throw and Throws Keywords
  • Exception Methods

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams

Prerequisites

  • No prior knowledge is required about the Java programming language
  • Basic computer knowledge
Transcript

Hi there. In this video, we'll 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 method 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 make some examples with useful methods of Throwable Class. In ExceptionHandling project, 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 0, we throw ArithmeticException by using the throw keyword with no constructor message. If the value of variable y is not 0, 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. In this catch block, we display the "Divider cannot be equal to zero" message by using the print method. Okay, let's run the code. Catch block handles the exception and the "Divider cannot 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 method of 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 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. 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 cannot 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 methods is one of the important methods used. If you want, let's use this as the last and end of the lesson. As you can see, the error message is briefly printed on the console screen. Yes, those 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
512
Courses
47
Learning Paths
3

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.

Covered Topics