try block problem in java..
-
sir/mam i want to know if i can put the try block directly in a class... as class a { try { methods; } catch() { } }
No, a try/catch block has to be inside a method. What are you trying to achieve exactly ?
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
No, a try/catch block has to be inside a method. What are you trying to achieve exactly ?
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
thanks cedric for your reply. If you don't mind can you please explain why we can't put the try/catch block directly in a class. why it is required to put it in a method.
For the same reason why you can't put code directly in a class: it is by design. If you would be able to put code directly in your class (not in a method), what would that mean ? A try/catch block is meant to surround code that could potentially fail. If you put it directly in a class, it doesn't surround any code.
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
thanks cedric for your reply. If you don't mind can you please explain why we can't put the try/catch block directly in a class. why it is required to put it in a method.
The
try{}catch{}
mechanism is specifically for catching exceptions thrown within a block of code, i.e inside a method. Putting such a mechanism in a class has no meaning as it includes things like variable definitions, overloaded methods etc.The best things in life are not things.
-
The
try{}catch{}
mechanism is specifically for catching exceptions thrown within a block of code, i.e inside a method. Putting such a mechanism in a class has no meaning as it includes things like variable definitions, overloaded methods etc.The best things in life are not things.
-
For the same reason why you can't put code directly in a class: it is by design. If you would be able to put code directly in your class (not in a method), what would that mean ? A try/catch block is meant to surround code that could potentially fail. If you put it directly in a class, it doesn't surround any code.
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++Cedric Moonen wrote:
If you would be able to put code directly in your class (not in a method), what would that mean ?
That it was static.
class MyClass {
{
System.out.println("This is a static code block");
}}
Hopefully no-one will actually write a class like that minimalist example, but static code blocks are there for a reason and can be useful under certain circumstances.
-
Cedric Moonen wrote:
If you would be able to put code directly in your class (not in a method), what would that mean ?
That it was static.
class MyClass {
{
System.out.println("This is a static code block");
}}
Hopefully no-one will actually write a class like that minimalist example, but static code blocks are there for a reason and can be useful under certain circumstances.
-
David Skelly wrote:
under certain circumstances.
...under very certain circumstances. I've just cleaned out that stuff out of one of my projects here. A former student used to make (like?) such crucial stuff.
regards Torsten I never finish anyth...
I once saw world war III break out over the rights and wrongs of static code blocks between an ex-Smalltalk OO purist and an ex-C++ hacker. No quarter asked or given on either side. It's like anything: give someone a hammer and they will find a way to hit themselves on the head with it.
-
sir/mam i want to know if i can put the try block directly in a class... as class a { try { methods; } catch() { } }
The code you write inside a class are NOT "executable", they're just declarations like variables and methods. The
try {} catch {}
block is designed to trap errors in executable code. Since only methods contain executable code,try {} catch {}
can be placed inside methods only.