Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. try block problem in java..

try block problem in java..

Scheduled Pinned Locked Moved Java
javahelp
10 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gateway23
    wrote on last edited by
    #1

    sir/mam i want to know if i can put the try block directly in a class... as class a { try { methods; } catch() { } }

    C L 2 Replies Last reply
    0
    • G gateway23

      sir/mam i want to know if i can put the try block directly in a class... as class a { try { methods; } catch() { } }

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      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++

      G 1 Reply Last reply
      0
      • C Cedric Moonen

        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++

        G Offline
        G Offline
        gateway23
        wrote on last edited by
        #3

        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.

        C L 2 Replies Last reply
        0
        • G gateway23

          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.

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          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++

          D 1 Reply Last reply
          0
          • G gateway23

            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.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            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.

            G 1 Reply Last reply
            0
            • L Lost User

              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.

              G Offline
              G Offline
              gateway23
              wrote on last edited by
              #6

              Thanks cedric and richrd. you solve my problem. thanks a lot.... :)

              1 Reply Last reply
              0
              • C Cedric Moonen

                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++

                D Offline
                D Offline
                David Skelly
                wrote on last edited by
                #7

                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.

                T 1 Reply Last reply
                0
                • D David Skelly

                  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.

                  T Offline
                  T Offline
                  TorstenH
                  wrote on last edited by
                  #8

                  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...

                  D 1 Reply Last reply
                  0
                  • T TorstenH

                    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...

                    D Offline
                    D Offline
                    David Skelly
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    • G gateway23

                      sir/mam i want to know if i can put the try block directly in a class... as class a { try { methods; } catch() { } }

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups