Industry-relevant training in Business, Technology, and Design to help professionals and graduates upskill for real-world careers.
Fun, engaging games to boost memory, math fluency, typing speed, and English skillsβperfect for learners of all ages.
Enroll to start learning
Youβve not yet enrolled in this course. Please enroll for free to listen to audio lessons, classroom podcasts and take mock test.
Read a summary of the section's main ideas. Choose from Basic, Medium, or Detailed.
In this section, we focus on the 'throw' keyword in Java, which allows developers to manually raise exceptions, providing control over error handling. An example of raising an ArithmeticException is presented to illustrate its usage.
In Java, exceptions can be raised manually using the throw
keyword. This is particularly useful when you want to enforce specific conditions in your code and handle them as exceptions. By raising exceptions manually, you can provide more meaningful error messages to users and enhance the robustness of your application.
Dive deep into the subject with an immersive audiobook experience.
Signup and Enroll to the course for listening the Audio Book
throw new ExceptionType("message");
The throw
statement in Java is used to manually raise an exception. This means you can create an exception and signal that something unexpected has occurred within your code. The exception being thrown can either be a built-in Java exception or a custom exception that you define yourself.
Imagine you're a teacher. If a student doesnβt follow the class rules, you might give them a warning. Similarly, using throw
, you tell the program: 'Thereβs a problem here!' just like you would alert a student about their behavior.
Signup and Enroll to the course for listening the Audio Book
public class ThrowExample {
public static void main(String[] args) {
int age = 15;
if (age < 18) {
throw new ArithmeticException("You are underaged!");
}
}
}
In this example, we have a class named ThrowExample
. Inside the main
method, there's a variable age
set to 15. The code checks if age
is less than 18. If it is, it raises an ArithmeticException
with a message saying, 'You are underaged!'. This is an example of how you can raise exceptions to enforce certain rules in your program.
Think of this like a bouncer at a club checking IDs. If someone tries to enter who is underage, the bouncer won't let them in and may point out, 'You are underaged!' Just as the bouncer takes action to ensure no one underage enters, our code takes action to prevent invalid situations.
Signup and Enroll to the course for listening the Audio Book
π§Ύ Output:
Exception in thread "main" java.lang.ArithmeticException: You are underaged!
When the throw
statement is executed in the previous example, the program does not continue its normal flow. Instead, it raises the ArithmeticException
which indicates that the code has encountered a problem due to the 'underage' condition. The output gives a clear message about what went wrong, helping developers or users to understand the issue.
This is akin to receiving a rejection note when you attempt to get into the club. The note specifies why you were turned away; in our case, the note clearly states 'You are underaged!', enabling you to understand what needs to be resolved.