Hello Oracle Users,
I have made a program
to calculate user date.
it works partali.
the problem is i added to it a catch function
that should catch errors.
the function has a try block and a catch block
and is located in anthor file called 'Err.java'
a refrence to the 'Err.java' file is "Err test_err = new Err();"
i added to the main java file called 'InputDate_catch.java'
also a 'try' and 'catch' statments
after the end of the try loop
and in the catch statment(in the main file)
i called the 'Err.java'(isDateFailed = test_err.err();)
where 'isDateFailed' is the boolen that should return true if error ocurred.
the function is called and all and all it does is return the "isDateFailed"
that's in the try block, and does not enters the catch statment
that are in the function, even if there are errors.
In short, My question is how can i pass the errors that eccours in the main program
to the function(Err.java)?
note: all this code is beetwen a 'do{' 'While{' loop,
becuase I need it to loop in a specfic place if there's an error,
and I need to redo it in specific places in the program.
Here is the Code for 'InputDate_catch.java':
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.time.*;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.time.format.DateTimeFormatter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
*
* @author User
*/
public class InputDate_catch {
//private static Object p;
//private static Object p.getYears;
/**
* @param args the command line arguments
* @throws java.lang.Exception in main metohod
*/
public static void main(String[] args) throws Exception {
// TODO code application logic here
boolean isDateFailed=false;
Err test_err = new Err();
String age;
do{
try {
isDateFailed=false; // reset's "isDateFailed" to false before catch error
age = JOptionPane.showInputDialog(null,"dd.MM.yyyy By:", "Type date of birth", JOptionPane.QUESTION_MESSAGE);
//if erro acoured goes to 'catch statment' should return here with 'isDateFailed=true'
// }while(isDateFailed); //loops if defined as 'true' in catch statment
/* All sorts of code:
* ...
* ...
* ...
* ...
* ...
* ...
* ...
* ...
* ...
*/
int year_burn;
int birth_year;
int birth_months;
int days_burn;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
LocalDate d1 = LocalDate.parse(age,formatter);
LocalDate dNow = LocalDate.now(); // Current date
Period p = Period.between(d1,dNow);
long p2 = ChronoUnit.DAYS.between(d1,dNow);
long p3 = ChronoUnit.WEEKS.between(d1,dNow);
} // end of try statment
catch(Exception ex){
isDateFailed = test_err.err(); //catch function for error blocks, in seperate file called 'Err.java'
}
System.out.println("after age input and inside while loop isDateFailed= "+ isDateFailed); // for testing purposes only
}while(isDateFailed); //loops if defined as 'true' in catch statment
System.out.println("You are " + p.getYears() + " years, " + p.getMonths() +
" months, and " + p.getDays() +
" days old. (" + p2 + " days total), (" + p3 + " Total Weeks)");
/* all sorts of code:
* ...
* ...
* ...
* ...
* ...
* ...
* ...
* ...
* ...
* ...
* ...
* ...
* ...
* ...
* ...
*/
}
}
P.S: if you try to compile the main file 'InputDate_catch.java' it will show an error of undefined varibales:(p.) and (p2) and (p3)
I think it's becuase the varibles that define them are in 'do{' loop. and for some reason the rest of the program doesn't see them.
In this I need help also.
And Here is the file Called('Err.java') that compiles good:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author User
*/
import javax.swing.JOptionPane;
public class Err {
boolean isDateFailed;
String strngtest;
/**
* 'param' below should start with '@'
* ¶m Nerr should be 1 if error true
* ¶m isDateFailed should be true if error true
* @return Nerr returns 1 if error ocuured
* @throws java.lang.Exception should catch errors
*
*
*/
public boolean err() throws Exception { //after 'err()' maybe needed: 'throws Exception'
int Nerr;
boolean isDateFailed;
try{
isDateFailed=false;
//Nerr = 1;
//return Nerr;
return isDateFailed;
} // end of try statment
catch(NumberFormatException ex) {
System.err.println("NumberFormatException "+ ex.getMessage());
JOptionPane.showMessageDialog(null, ex.getMessage(), "System error in typed value", JOptionPane.ERROR_MESSAGE);
isDateFailed=true;
return isDateFailed;
}
catch(java.time.format.DateTimeParseException ex) {
System.err.println("DateFormatException "+ ex.getMessage());
JOptionPane.showMessageDialog(null, ex.getMessage(), "Please type Value by shown strucure, bad value", JOptionPane.ERROR_MESSAGE);
isDateFailed = true;
return isDateFailed;
}
catch(NullPointerException ex) {
System.err.println("NullPointerException "+ ex.getMessage());
JOptionPane.showMessageDialog(null, ex.getMessage() ,"System error missing value", JOptionPane.ERROR_MESSAGE);
isDateFailed = true;
return isDateFailed;
}
}
}
In short, I need help to learn how to pass erros to a function and that will catch it,
And how to use variables inside a 'do{' loop in a manner that they will still be seen outside the 'do{' loop.
Thank's a Lot,
Yaniv R.