exceptions and patterns

hi,

i created a bunch of exceptions as an exacmple

class MyException extends Exception{

}

and an interface to handel different ways to handel my exceptions say

interface HandelInterface {

handel(Exception exp);

}

i created a class that implements the interface

class GErrorHandel implements HandelInterface{

handel (Exception exp){

if (exp instanceof MyException)

System.out.println("My exception");

}

}

i created a factory to choose the required handeler and the factory is singleton but the method getInstance() returns an instance of the interface HandelInterface instead of returnning an instance of the factory (lets call the factory HandelerFactory).

then i used the handler and that's when i my problems rise up and shine :)

in a class A when i figur that there is a problem and i want to deal with it i would say

HandlerFactory.getInstance().handel();

in the method handel() i use the JOptionPane.showMessageDialog()

now what happens is that i get the message that i am supposed to get BUT in the console i get a huge error message which contains the stakc trace (i think) which i thought that i shouldn't get since i am dealing with the exception using the JOptionPane.showMessageDialog.

that was the first problem.. the second problem happen this way ..

my application does some operations then it starts creating the GUI, now if anything goes wrong causing the handel() to work before the GUI gets created i don't get the JOptionPane.showMessageDialog() message AND the application would stop without continuing with creating the GUI or anything , just stops .. but it doesn't print the stack trace !

what could be the problem and is there anyway to deal with it ?

thanks for helping

Musab

[1865 byte] By [malrawia] at [2007-9-28 10:56:17]
# 1
I think you are talking about the Java issues, it is simple, change your function call sequences, i wish you have good modularity :-)But ... do you really require the exception handling this way ?- Karthicraja
karthicrajaa at 2007-7-12 1:09:29 > top of Java-index,Other Topics,Patterns & OO Design...
# 2
>what could be the problem and is there anyway to deal with it ?Just as a guess....You do know that exceptions are only propogated in the current thread - correct? You have to catch it in the thread it happened in.
jschella at 2007-7-12 1:09:29 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

i know that sounded complicated :) ... but i am trying to design the application so that it can take a GUI or text user interface or whatever interface the programmer wants to implement .. so that's why i used the ErrorHandelerInterface so that the error messages will appear to the user in the way they should appear .. if it is GUI it should appear throuw the showmessage method .. if it is text then simply a System.out.println()

i am catchin the error where it is happening i think the proof is that there is an error message being shown when an error happens ..

it is true that i have like 5 function calls may be till the error happens but i am calling the error handling method ( which is handel() ) where it happens and when it happens and that's when i call the handel() ..

so i really don't know how i can change the sequance

any suggestions ?

thanks for helpping

Musab

malrawia at 2007-7-12 1:09:29 > top of Java-index,Other Topics,Patterns & OO Design...
# 4

> i am trying to design the application so that it

> can take a GUI or text user interface or whatever

> interface the programmer wants to implement

Have you considered using MVC (model/view/controller). In this pattern, the model is independant of the view (the view can be altered or replaced altogether and the model need not be changed). Typically the observer pattern is used to implement this independance.

zigvelveta at 2007-7-12 1:09:29 > top of Java-index,Other Topics,Patterns & OO Design...
# 5

i read about MVC i haven't tried it yet and i had some problems understanding it .. and i am almost done with the project (which is the application that i am having the problem with) i used a factory to decide which interface the user wants and it works fine .. it is just the exception thing that doesn't work perfectly.. i will try to use System.out.println() instead of the showmessage and see if i get the same problem or not ..

MeanWhile if anyone has any suggestion plz let me know i will try anything :)

thanks

malrawia at 2007-7-12 1:09:29 > top of Java-index,Other Topics,Patterns & OO Design...
# 6

ok i am just gussing here so dont curse me becuz i am learnning java :)

ok the handel() is not in a try catch block , could this be the problem ?

the way i have it is like this

if (expression) doWhatever

else ErrorHandelerInterface.getInstance().handel(new MyException());

if it is then how should i write it ?

thanks

malrawia at 2007-7-12 1:09:29 > top of Java-index,Other Topics,Patterns & OO Design...
# 7

your code should probably look like this:

try {

... do something

if ( some condition) {

throw new Exception("Something went wrong");

}

... do something else (won't happen if you throw the exception)

} catch (Exception ex){

ErrorHandelerInterface.getInstance().handel(ex);

}

spielera at 2007-7-12 1:09:29 > top of Java-index,Other Topics,Patterns & OO Design...