목록[JAVA]/11.예외처리 (15)
Beny's Study
package com.kh.ex02.user_exception; public class UseException { public static void main(String[] args) { String userID = "test"; String userPW = "1234"; String inputID = "test"; String inputPW = "1234"; try { if(userID.equals(inputID) == false) { throw new LoginException("아이디가 맞지 않습니다!");//괄호안의 ""가 cause } if(userPW.equals(inputPW) == false) { throw new LoginException("비밀번호가 맞지 않습니다!"); } System..
throws - 메서드가 발생하면 이러이러한 예외가 발생할 수 있다. -예외발생시 상위 메소드에게 책임을 전가하는 키워드 - 단, main에서의 예외는 프로그램 종료를 의미 throw -예외발생, 예외를 만들어서 던지는 문장(000라는 예외발생) -예외발생시 상위 메소드에게 책임을 전가하는 키워드 -단, main에서의 예외는 프로그램 종료를 의미 import java.io.File; import java.io.IOException; public class UseThrows { public static void method() throws IOException { File file = new File("./Test"); file.createNewFile(); // IOException 발생, 대표적으로 명..
1. 산술예외 ArithmeticException int a = 10 / 0; 2.배열범위를 뛰어넘는 ArrayIndexOutOfBoundsException int[] array = new int[10]; array[12] = 10; 오류이유 : 공간은 처음에 10개로 생성했는데 밑에서 12로 받아서 오류 3.음수예외 NegativeArraySizeException array = new int[-1]; 4.null예외 NullPointerException array = null; array[10] = 10; 오류이유: null인 상태에서 접근하려고 하니까 오류! 5. 형변환예외 ClassCastException Object obj = new Object(); String str = (String)obj;..
SimpleDateFormat df = new SimpleDateFormat("yyy-MM-dd hh:mm:ss"); //1. SimpleDateFormat으로 날짜포멧지정 System.out.println(df.format(file1.lastModified())); //2. lastModified메소드 : 최종수정날짜 //long으로 땡겨 오는 이유 : 보통 time일떄 long로 떙겨옴
코드 package com.kh.ex01.file; import java.io.File; import java.text.SimpleDateFormat; import java.util.Arrays; public class FileBasic { public static void main(String[] args) { File file1 = new File("./covid.txt"); // 상대경로(현재파일로부터의 경로),file1에 저장 File file2 = new File(file1.getAbsolutePath()); // 절대경로(c드라이브 부터의 경로), file2에 저장 File newFile = new File("./covid2.txt"); file1.renameTo(newFile); //위에 새..