Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Creating Custom Exceptions | Advanced Exception Handling
Quizzes & Challenges
Quizzes
Challenges
/
Exceptions and Error Handling in Java

bookCreating Custom Exceptions

As you build more complex Java applications, you may encounter error situations that are unique to your program's logic. While Java provides many built-in exceptions, sometimes none of them clearly describe the specific problem you want to report. In these cases, you can create your own custom exceptions. Custom exceptions allow you to give meaningful names to error conditions, making your code easier to understand, maintain, and debug. By defining exceptions that are tailored to your application's needs, you help other developers (and your future self) quickly identify and handle specific error scenarios.

To create a custom exception, you define a new class that extends either the Exception class or the RuntimeException class. If your exception should be checked (that is, the compiler forces you to handle it with a try-catch block or declare it with throws), extend Exception. If it should be unchecked (not enforced by the compiler), extend RuntimeException. Your custom exception can include constructors, fields, and methods just like any other class, but it's common to provide at least a constructor that accepts a message describing the error.

Main.java

Main.java

copy
123456789101112131415161718192021222324
package com.example; class InvalidAgeException extends Exception { public InvalidAgeException(String message) { super(message); } } public class Main { public static void checkAge(int age) throws InvalidAgeException { if (age < 18) { throw new InvalidAgeException("Age must be at least 18."); } System.out.println("Access granted. Age is valid."); } public static void main(String[] args) { try { checkAge(15); } catch (InvalidAgeException e) { System.out.println("Caught exception: " + e.getMessage()); } } }

In the code above, you define a custom checked exception by creating the InvalidAgeException class, which extends Exception. This class has a constructor that accepts an error message. The checkAge method throws an InvalidAgeException if the provided age is less than 18.

In the main method, you call checkAge(15), which triggers the exception. The try-catch block catches the exception and prints its message. This pattern shows how you can use custom exceptions to handle specific error conditions in a clear and organized way.

question mark

What must you do to create a custom checked exception in Java?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 1

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Awesome!

Completion rate improved to 10

bookCreating Custom Exceptions

Svep för att visa menyn

As you build more complex Java applications, you may encounter error situations that are unique to your program's logic. While Java provides many built-in exceptions, sometimes none of them clearly describe the specific problem you want to report. In these cases, you can create your own custom exceptions. Custom exceptions allow you to give meaningful names to error conditions, making your code easier to understand, maintain, and debug. By defining exceptions that are tailored to your application's needs, you help other developers (and your future self) quickly identify and handle specific error scenarios.

To create a custom exception, you define a new class that extends either the Exception class or the RuntimeException class. If your exception should be checked (that is, the compiler forces you to handle it with a try-catch block or declare it with throws), extend Exception. If it should be unchecked (not enforced by the compiler), extend RuntimeException. Your custom exception can include constructors, fields, and methods just like any other class, but it's common to provide at least a constructor that accepts a message describing the error.

Main.java

Main.java

copy
123456789101112131415161718192021222324
package com.example; class InvalidAgeException extends Exception { public InvalidAgeException(String message) { super(message); } } public class Main { public static void checkAge(int age) throws InvalidAgeException { if (age < 18) { throw new InvalidAgeException("Age must be at least 18."); } System.out.println("Access granted. Age is valid."); } public static void main(String[] args) { try { checkAge(15); } catch (InvalidAgeException e) { System.out.println("Caught exception: " + e.getMessage()); } } }

In the code above, you define a custom checked exception by creating the InvalidAgeException class, which extends Exception. This class has a constructor that accepts an error message. The checkAge method throws an InvalidAgeException if the provided age is less than 18.

In the main method, you call checkAge(15), which triggers the exception. The try-catch block catches the exception and prints its message. This pattern shows how you can use custom exceptions to handle specific error conditions in a clear and organized way.

question mark

What must you do to create a custom checked exception in Java?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 1
some-alt