Throwing Exceptions
When you need to indicate that something has gone wrong in your Java program, you can use the throw statement. The throw statement allows you to intentionally generate an exception at any point in your code. This is especially useful when you want to signal that a method has received invalid input or when a specific error condition occurs that your code cannot handle directly.
throw new Exception();
Throwing exceptions is an essential part of robust error handling. By throwing an exception, you alert the calling code that something unexpected has happened, and you hand over control so that the error can be dealt with appropriately—often in a catch block or by propagating the exception further up the call stack. This approach helps you separate normal execution flow from error handling logic, making your code easier to read and maintain.
Main.java
12345678910111213141516171819package com.example; public class Main { public static void main(String[] args) { try { int age = -5; checkAge(age); } catch (IllegalArgumentException e) { System.out.println("Caught exception: " + e.getMessage()); } } public static void checkAge(int age) { if (age < 0) { throw new IllegalArgumentException("Age cannot be negative: " + age); } System.out.println("Valid age: " + age); } }
In this example, the checkAge method is designed to validate its input. If you call checkAge with a negative value, the method uses the throw statement to create and throw a new IllegalArgumentException. The exception includes a message explaining what went wrong. This immediately stops normal execution of the method, and control jumps to the nearest matching catch block—in this case, in the main method.
By throwing an exception in this way, you make it clear to anyone using your method that certain inputs are not allowed, and you provide a standard way for the calling code to react to the error.
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Can you show me the full example of the `checkAge` method and how it's used?
What are some common exceptions to throw in Java for different error conditions?
How does exception propagation work in Java?
Awesome!
Completion rate improved to 10
Throwing Exceptions
Veeg om het menu te tonen
When you need to indicate that something has gone wrong in your Java program, you can use the throw statement. The throw statement allows you to intentionally generate an exception at any point in your code. This is especially useful when you want to signal that a method has received invalid input or when a specific error condition occurs that your code cannot handle directly.
throw new Exception();
Throwing exceptions is an essential part of robust error handling. By throwing an exception, you alert the calling code that something unexpected has happened, and you hand over control so that the error can be dealt with appropriately—often in a catch block or by propagating the exception further up the call stack. This approach helps you separate normal execution flow from error handling logic, making your code easier to read and maintain.
Main.java
12345678910111213141516171819package com.example; public class Main { public static void main(String[] args) { try { int age = -5; checkAge(age); } catch (IllegalArgumentException e) { System.out.println("Caught exception: " + e.getMessage()); } } public static void checkAge(int age) { if (age < 0) { throw new IllegalArgumentException("Age cannot be negative: " + age); } System.out.println("Valid age: " + age); } }
In this example, the checkAge method is designed to validate its input. If you call checkAge with a negative value, the method uses the throw statement to create and throw a new IllegalArgumentException. The exception includes a message explaining what went wrong. This immediately stops normal execution of the method, and control jumps to the nearest matching catch block—in this case, in the main method.
By throwing an exception in this way, you make it clear to anyone using your method that certain inputs are not allowed, and you provide a standard way for the calling code to react to the error.
Bedankt voor je feedback!