BAEL-3146 Source code for article: Checked and Unchecked Exceptions in Java
This commit is contained in:
		
							parent
							
								
									8205672b29
								
							
						
					
					
						commit
						0630b2dffc
					
				| @ -0,0 +1,43 @@ | |||||||
|  | package com.baeldung.exceptions; | ||||||
|  | 
 | ||||||
|  | import java.io.File; | ||||||
|  | import java.io.FileInputStream; | ||||||
|  | import java.io.FileNotFoundException; | ||||||
|  | 
 | ||||||
|  | public class CheckedUncheckedExceptions { | ||||||
|  |     public static void checkedExceptionWithThrows() throws FileNotFoundException { | ||||||
|  |         File file = new File("not_existing_file.txt"); | ||||||
|  |         FileInputStream stream = new FileInputStream(file); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public static void checkedExceptionWithTryCatch() { | ||||||
|  |         File file = new File("not_existing_file.txt"); | ||||||
|  |         try { | ||||||
|  |             FileInputStream stream = new FileInputStream(file); | ||||||
|  |         } catch (FileNotFoundException e) { | ||||||
|  |             e.printStackTrace(); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public static int divideByZero() { | ||||||
|  |         int numerator = 1; | ||||||
|  |         int denominator = 0; | ||||||
|  |         return numerator / denominator; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public static void checkFile(String fileName) throws IncorrectFileNameException { | ||||||
|  |         if (fileName == null || fileName.isEmpty()) { | ||||||
|  |             throw new NullOrEmptyException("The filename is null."); | ||||||
|  |         } | ||||||
|  |         if (!isCorrectFileName(fileName)) { | ||||||
|  |             throw new IncorrectFileNameException("Incorrect filename : " + fileName); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     private static boolean isCorrectFileName(String fileName) { | ||||||
|  |         if (fileName.equals("wrongFileName.txt")) | ||||||
|  |             return false; | ||||||
|  |         else | ||||||
|  |             return true; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,13 @@ | |||||||
|  | package com.baeldung.exceptions; | ||||||
|  | 
 | ||||||
|  | public class IncorrectFileNameException extends Exception { | ||||||
|  |     private static final long serialVersionUID = 1L; | ||||||
|  | 
 | ||||||
|  |     public IncorrectFileNameException(String errorMessage) { | ||||||
|  |         super(errorMessage); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public IncorrectFileNameException(String errorMessage, Throwable thr) { | ||||||
|  |         super(errorMessage, thr); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,14 @@ | |||||||
|  | package com.baeldung.exceptions; | ||||||
|  | 
 | ||||||
|  | public class NullOrEmptyException extends RuntimeException { | ||||||
|  | 
 | ||||||
|  |     private static final long serialVersionUID = 1L; | ||||||
|  | 
 | ||||||
|  |     public NullOrEmptyException(String errorMessage) { | ||||||
|  |         super(errorMessage); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public NullOrEmptyException(String errorMessage, Throwable thr) { | ||||||
|  |         super(errorMessage, thr); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,54 @@ | |||||||
|  | package com.baeldung.exceptions; | ||||||
|  | 
 | ||||||
|  | import static org.junit.jupiter.api.Assertions.assertThrows; | ||||||
|  | 
 | ||||||
|  | import java.io.FileNotFoundException; | ||||||
|  | 
 | ||||||
|  | import org.junit.jupiter.api.Assertions; | ||||||
|  | import org.junit.jupiter.api.Test; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * Tests the {@link CheckedUncheckedExceptions}. | ||||||
|  |  */ | ||||||
|  | public class CheckedUncheckedExceptionsUnitTest { | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void whenFileNotExist_thenThrowException() { | ||||||
|  |         assertThrows(FileNotFoundException.class, () -> { | ||||||
|  |             CheckedUncheckedExceptions.checkedExceptionWithThrows(); | ||||||
|  |         }); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void whenTryCatchExcetpion_thenSuccess() { | ||||||
|  |         try { | ||||||
|  |             CheckedUncheckedExceptions.checkedExceptionWithTryCatch(); | ||||||
|  |         } catch (Exception e) { | ||||||
|  |             Assertions.fail(e.getMessage()); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void whenDivideByZero_thenThrowException() { | ||||||
|  |         assertThrows(ArithmeticException.class, () -> { | ||||||
|  |             CheckedUncheckedExceptions.divideByZero(); | ||||||
|  |         }); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void whenInvalidFile_thenThrowException() { | ||||||
|  | 
 | ||||||
|  |         assertThrows(IncorrectFileNameException.class, () -> { | ||||||
|  |             CheckedUncheckedExceptions.checkFile("wrongFileName.txt"); | ||||||
|  |         }); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Test | ||||||
|  |     public void whenNullOrEmptyFile_thenThrowException() { | ||||||
|  |         assertThrows(NullOrEmptyException.class, () -> { | ||||||
|  |             CheckedUncheckedExceptions.checkFile(null); | ||||||
|  |         }); | ||||||
|  |         assertThrows(NullOrEmptyException.class, () -> { | ||||||
|  |             CheckedUncheckedExceptions.checkFile(""); | ||||||
|  |         }); | ||||||
|  |     } | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user