Question about exception handling - Help needed
Hi,
Could someone please breifly explain to me how I can handle exceptions from multiple classes, I cant find anything on the Internet that uses multiple classes.
This is my catch statement to handle the exception within my Main program (below), however I want to handle an exception from another class. I understand I could use the .getMessage, but I have three classes and Im thinking this method will only work with one?
Could someone please explain this to me?
Catch within my Main program:
Code:
catch (Exception e)
{
System.err.println("Error: This is the Exception");
}
Thanks,
Ben
Re: Question about exception handling - Help needed
I'm not sure what exactly do you want to do and what`s your question :s:
You can catch several exceptions and you can treat them differently
Code:
catch ( SQLException e )
{
...
}
catch ( IOException e )
{
....
}
In some cases, you want to do the same (e.g. logging the exception) in both cases, since Java 7 there is a multiple catch
Code:
catch ( SQLException | IOException e )
{
....
}
If this doesn`t answer your question, maybe this helps: Lesson: Exceptions (The Java™ Tutorials > Essential Classes)
Re: Question about exception handling - Help needed
Also, you want to printStackTrace() for the exception otherwise you won't have a clue where it was thrown from.